Last Updated: February 25, 2016
· mokhtar ashour

Parsing Xml in C#

Assume we want to extract some objects from an xml file using C#.

the xml file:

<Annotation text="Brazilian oil giant Petrobras and U.S. oilfield service company Halliburton have signed a technological cooperation agreement, Petrobras announced Monday.">
<Resource URI="http://dbpedia.org/resource/Oil" support="803" types="" surfaceForm="oil" offset="10" similarityScore="0.10925747454166412" percentageOfSecondRank="0.43448779313556946"/>
<Resource URI="http://dbpedia.org/resource/Company" support="427" types="" surfaceForm="company" offset="56" similarityScore="0.10083067417144775" percentageOfSecondRank="0.562207867961491"/>

we need the URI and surfaceForm pair from the document.

the C# code to do the parsing:

using  System.Xml.Linq

//some code here.           
 XDocument doc = XDocument.Parse("XmlFileName.xml");
 var Resources = doc.Descendants("Resource").Select(resource => new
                URI = resource.Attribute("URI").Value,
                Entity = resource.Attribute("surfaceForm").Value,
//to arrange the list of objects in a dictionary

Dictionary<string, Uri> entities = new Dictionary<string, Uri>();

foreach (var resource in Resources)
        entities.Add(resource.Entity, new Uri(resource.URI));

note that the resource.Attribute will work with the xml element attributes, if you want it to access elements, then resource.Element it is :-)