.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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
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: 5 stars5 stars5 stars5 stars5 stars / 2
    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

    - Using CrystalReportViewer to Display Crystal...
    - Creating Summary .Net Crystal Reports
    - More on Commands, Input and the WPF
    - Grouping and Aggregating When Querying LINQ ...
    - Commands, Input and the WPF
    - Keyboard and Ink Input with WPF
    - Mouse Input and the WPF
    - Input with Windows Presentation Foundation
    - Introducing LINQ with XML and Databases
    - An Introduction to LINQ
    - Querying LINQ to SQL: Basics
    - Completing a Simple Storefront with LINQ
    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT