Geeks With Blogs


INauseous() Shawn Cicoria - Solution Architect, Craftsman and Artisan - INauseous() - Main Blog Here:

Recently I had to deal with some XML supplied to a development group that wasn't the greatest in structure.  For one, there was inconsistent use of namespaces and each repeating element had the namespace duplicated.

The goal was to retrieve from the XML various element values for a business object.

So, using LINQ to XML, there's some pretty neat ways to go about this.  The most succinct that I came up with is below - of course it could be tiny bit more succinct in terms of not using locally scoped variable names but it's there for readability

The trick is use of the XNamespace type, which has an implied constructor, then just passing the namespace qualifier on the LINQ statement as a concatenated variable.


   12 public void Test2(string xmlContents)

   13 {

   14     XDocument doc = XDocument.Load(new StringReader xmlContents));

   15     XNamespace ns0 = "http://mysimpleXml/rootStuff";

   16     XNamespace ns1 = "http://mysimpleXml/customer";


   18     var query = from c in doc.Elements( ns0 + "myRoot" ).Elements( ns1 + "customer" )

   19                 select c;



   22     foreach (XElement order in query)

   23     {

   24         string firstname = (from c in order.Elements( ns1 + "firstName" ) select c).First().Value;

   25         string lastname = (from c in order.Elements( ns1 + "lastName" ) select c).First().Value;


   27         Console.WriteLine("firstName: " + firstname);

   28         Console.WriteLine("lastName: " + lastname);


   30     }


   32 }

The sample XML is below


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

    2 <myRoot xmlns="http://mysimpleXml/rootStuff">

    3   <customer xmlns="http://mysimpleXml/customer">

    4     <firstName>John</firstName>

    5     <lastName>Lennon</lastName>

    6   </customer>

    7   <customer xmlns="http://mysimpleXml/customer">

    8     <firstName>Ringo</firstName>

    9     <lastName>Starr</lastName>

   10   </customer>

   11 </myRoot>

Posted on Monday, November 24, 2008 7:07 PM LINQ | Back to top

Comments on this post: LINQ to XML - retrieving Element values with varying namespaces...

# re: LINQ to XML - retrieving Element values with varying namespaces...
Requesting Gravatar...
You should try it in VB, it is even easier/better ;)

The example you have works because of a cool operator overload on the XNamespace class.
Left by Don Demsak on Nov 24, 2008 7:51 PM

Comments have been closed on this topic.
Copyright © Shawn Cicoria | Powered by: