.NET
  Home arrow .NET arrow Page 3 - Introducing LINQ with XML and Databases
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Introducing LINQ with XML and Databases
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2008-06-16

    Table of Contents:
  • Introducing LINQ with XML and Databases
  • Querying the Database
  • LINQ to XML
  • XML Literals in Visual Basic

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Introducing LINQ with XML and Databases - LINQ to XML


    (Page 3 of 4 )

    Now let's take a look at LINQ to XML. LINQ to XML gives us a new way to represent and work with XML. Let's examine this through example. Suppose we want to represent a group of people. For each person, we want to represent a name and an age. So, an XML representation of all of this data might look like this:


    <?xml version="1.0" encoding="utf-8" standalone="yes"?>

    <people>

    <person>

    <name>Bob</name>

    <age>35</age>

    </person>

    <person>

    <name>Henry</name>

    <age>43</age>

    </person>

    <person>

    <name>Joe</name>

    <age>22</age>

    </person>

    <person>

    <name>Chuck</name>

    <age>29</age>

    </person>

    </people>


    In order to demonstrate the new API provided by LINQ to XML, let's actually build the above XML document first. You'll need to import the System.Xml.Linq namespace to get started. This namespace provides a variety of classes, of which we'll look at a few. The first is XDocument, which represents an XML document. Our entire document above can be contained within an XDocument class. The second is XDeclaration, which can contain the first line of our document – the XML declaration. The third is XElement, which represents an element. The body of our document can be represented by nesting XElement objects.

    When creating, for example, an XElement object, we're able to pass in other child elements as objects. For example, here, we create a single person:


    XElement bob = new XElement("person",

     new XElement("name", "Bob"),

     new XElement("age", "35")

    );


    Dim bob As New XElement("person", _

     New XElement("name", "Bob"), _

     New XElement("age", "35") _

    )


    If we print out the object, the proper XML is outputted:


    Console.WriteLine(bob);


    <person>

    <name>Bob</name>

    <age>35</age>

    </person>


    As you can see, building XML using the API is pretty straightforward. Let's now build the entire XML document:


    XDocument document = new XDocument(

     new XDeclaration("1.0", "utf-8", "yes"),

     new XElement("people",

     new XElement("person",

     new XElement("name", "Bob"),

     new XElement("age", "35")),

     new XElement("person",

     new XElement("name", "Henry"),

     new XElement("age", "43")),

     new XElement("person",

     new XElement("name", "Joe"),

     new XElement("age", "22")),

     new XElement("person",

     new XElement("name", "Chuck"),

     new XElement("age", "29"))

    )

    );


    Dim document As New XDocument( _

     New XDeclaration("1.0", "utf-8", "yes"), _

     New XElement("people", _

     New XElement("person", _

     New XElement("name", "Bob"), _

     New XElement("age", "35")), _

     New XElement("person", _

     New XElement("name", "Henry"), _

     New XElement("age", "43")), _

     New XElement("person", _

     New XElement("name", "Joe"), _

     New XElement("age", "22")), _

     New XElement("person", _

     New XElement("name", "Chuck"), _

     New XElement("age", "29")) _

    ) _

    )


    The result is actually quite readable and manageable, which is, of course, a very good thing. If we want to, we can save our document for future use:


    document.Save("people.xml");


    We can now query our document using a combination of the query syntax and the classes that we just went over. Let's retrieve everyone over the age of thirty:


    var overThirty = from p in document.Descendants("person")

     where (int)p.Element("age") > 30

     select new

    {

     Name = (string)p.Element("name"),

     Age = (int)p.Element("age")

     };


    Dim overThirty = From p In document.Descendants("person") _

     Where CInt(p.Element("age")) > 30 _

     Select New With { _

     .Name = CStr(p.Element("name")), _

     .Age = CInt(p.Element("age")) _

     }


    Unfortunately, we do have to cast each element to the proper type.

    We're also able to go further and create a new XML document by querying an old one. For example, suppose wanted to create an entirely new XML document based on the people over the age of thirty. To do this, we just have to drop a query where we want the elements to go:


    XDocument overThirtyDocument = new XDocument(

     new XDeclaration("1.0", "utf-8", "yes"),

     new XElement("people",

     from p in document.Descendants("person")

     where (int)p.Element("age") > 30

     select p

    )

    );


    Dim overThirtyDocument As New XDocument( _

     New XDeclaration("1.0", "utf-8", "yes"), _

     New XElement("people", _

     From p In document.Descendants("person") _

     Where CInt(p.Element("age")) > 30 _

     Select p _

    ) _

    )


    More .NET Articles
    More By Peyton McCullough


       · Hello, all,This is a continuation of my...
     

    .NET ARTICLES

    - Building Applications with Windows Workflow ...
    - Building the Data and Business Layers Using ...
    - The Transformed XML Explorer in MFC
    - List Control and Property Grid with the MFC ...
    - Font, Shell and Masked Edit Controls for MFC
    - Color, Link and Image Editor Controls for M...
    - New Controls for MFC
    - The Windows Ribbon Framework
    - Markup Language for the Ribbon Framework
    - Visually Upgrade Your MFC Project
    - New Features for the Statusbar in MFC
    - Working with the Statusbar in MFC
    - Iron Speed Design v60 Review
    - Binary and XML Serialization
    - Using CrystalReportViewer to Display Crystal...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek