RSS

Category Archives: XML

Reading an XML file using Linq to XML

A quick and post and more for my own reference but I recently needed to read some elements from an XML document which can be accomplished much easier these days using Linq to XML.

So if I have a simple XML document :

Sample XML document

I can read this using Linq as below (language is C#) :

C# routine to parse the XML document using Linq

In this method I am first loading the XML file into an XDocument Object found under the System.XML.Linq namespace.

I am then using Linq to return a collection of Items which represent the Parent ‘Client’ Nodes, using the Descendants function.

Then we can loop though each Client Node and simply extract the Element Values for the KEY and POSITION Nodes.  Its also worth noticing in this example that I have included an attribute at the CLIENT Node level.  Using Attributes().First() I am requesting the first attribute found attached to my CLIENT object.

Like I mentioned nothing difficult here but a handy little reference

 
Leave a comment

Posted by on June 22, 2012 in C#, Linq, Uncategorized, XML

 

Querying Properties within csproj files using XML

Another short and sweet post.  As part of the project I’m currently working on, I needed to use the value stored in the RootNamespace Node within the Project file (.csproj).

Now as a csproj file is essentially an XML document we can use the XML libraries in Visual studio to obtain the values we are after.

Heres the code I used to return the value from the RootNamespace Element.  (Note: Remember to set the Namespace!!)

 

Querying a csproj file

 

 

 
Leave a comment

Posted by on July 20, 2011 in C#, XML