Attention: We are retiring the ASP.NET Community Blogs. Learn more >

LINQ to XML over conventional ways

Imagine you have this XML file:

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

    2 <NewDataSet>

    3   <Table>

    4     <ID>bc307ed4-4a5b-479e-9298-0831e077850c</ID>

    5     <ResourceKey>0000001</ResourceKey>

    6     <CultureCode>en-GB</CultureCode>

    7     <Value>Save</Value>

    8   </Table>

    9   <Table>

   10     <ID>195565c8-8020-4347-910c-dedc41579e2a</ID>

   11     <ResourceKey>0000001</ResourceKey>

   12     <CultureCode>ms-MY</CultureCode>

   13     <Value>Simpan</Value>

   14   </Table>

   15   <Table>

   16     <ID>6d78c9df-6e32-4e02-b784-f78c044bb086</ID>

   17     <ResourceKey>0000002</ResourceKey>

   18     <CultureCode>en-GB</CultureCode>

   19     <Value>Full Name</Value>

   20   </Table>

   21   <Table>

   22     <ID>87913546-42a8-4d47-bf61-2ca39ce3c986</ID>

   23     <ResourceKey>0000002</ResourceKey>

   24     <CultureCode>ms-MY</CultureCode>

   25     <Value>Name Penuh</Value>

   26   </Table>

   27   <Table>

   28     <ID>0d4dbea5-519a-4b81-8165-8fbc110ead6a</ID>

   29     <ResourceKey>0000003</ResourceKey>

   30     <CultureCode>en-GB</CultureCode>

   31     <Value>I love you</Value>

   32   </Table>

   33   <Table>

   34     <ID>c6f55881-e970-4579-81e9-04b552651d4f</ID>

   35     <ResourceKey>0000003</ResourceKey>

   36     <CultureCode>ms-MY</CultureCode>

   37     <Value>Aku Cinta Pada Mu</Value>

   38   </Table>

   39 </NewDataSet>

I need to perform a query on this xml file and get all elements of specific culture code "ms-MY".

There were a lot of ways to handle this back then. I will name a few:

a. Use DataSet to read this file and convert to DataTables. I don't really like to use DataSets in my code.

b. Use XmlDocument and XmlElement.

c. Use XmlDocument, XPathNavigator, XPathExpression, XPathNodeIterator. I am able to iterate the innerxml, however I need to write codes to parse that innerxml

I found out that you can do this so much easier in LINQ to XML with XDocument and XElement.

Check the code below:

  115  XDocument doc = XDocument.Load(dataFileName);

  116 

  117             var result = from p in doc.Descendants("Table")

  118                         where (string)p.Element("CultureCode") == cultureCode

  119                         select new { ID = (string)p.Element("ID"), Value = (string)p.Element("Value") };

  120 

  121             foreach (var name in result)

  122                 MessageBox.Show(name.ID + " ... " + name.Value);

Few things to note for beginners to LINQ:

a. You need to cast p.Element("ID") to string, if not you will be getting the whole xml chunk

   25 <Value>Name Penuh</Value>

 Assuming you will see that in standard output

b. You need to assign an representation of (string)p.Element("ID") which in this context ID. If not you will get this error:

Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access. 

It is really easy and maybe you should consider LINQ to XML for querying and processing. Thanks.

1 Comment

  • Excellent article. I'm using LINQ for a simple XML file (help file references) and this is so much easier than the usual select nodes methods. To me the syntax is much "cleaner." =)

Comments have been disabled for this content.