Parsing XML in C#

February 6, 2011 | 1 Comment

Lately, I dealt with webservices with C# while working on bluekiwi’s Microsoft Office Connector. The bluekiwi API sends its reponses in XML format. I had originally used DataSets to store the content of the response though I soon realize I could work just as easily with the XML itself using the XElement class.

Here is an example.

The webservice response would look as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<xml>
<bkresponse>
<status>200</status>
<response>
<objects>
<object>
<id>1</id>
<title>objectTitle</title>
<objectAttributes>
<attr>value1</attr>
<attr>value2</attr>
</objectAttributes>
</object>
<object>
<!-- more objects -->
</object>
</objects>
</response>
</bkresponse>
</xml>

The difficulty with datasets was to query the value of a “node” using its name (ie “attr”) because each object could have several of them but they all had the same name…

If the xml above is the value of the variable named response, as obtained from the webservice, here is how it was converted to an XML object

1
2
3
4
5
System.IO.StringReader strxmlreader = new System.IO.StringReader(response);
XmlTextReader xmlReader = new XmlTextReader(strxmlreader);
xmlReader.Read();
result = xmlReader;
XDocument xml = XDocument.Load(result);

Working with this xml object is quite simple, here is how we can test the status of the response.

1
2
3
4
5
if (Int32.Parse(xml.Element("bkresponse").Element("status").Value) != 200)
{
this._requestError();
return;
}

It is just as easy to loop on each object node to extract the data from the response

1
2
3
4
5
6
7
8
foreach (XElement elt in xml.Element("bkresponse").Element("response").Element("objects").Elements("object"))
int id = Int32.Parse(elt.Element("id").Value);
string name = elt.Element("title").Value;
foreach (XElement eltType in elt.Element("objectAttributes").Elements("attr"))
// handle data
}
// Object myObject = new Object(id, name, attributes);
}

Simple enough, it done the trick for me.

Filed under: Computing, Development, Dot Net

Tagged with: , , , , , , ,

Liked this page?

Tweet it

Subscribe to the RSS feed or sign up for the newsletter now.

Posts related to Parsing XML in C#

1 Comment to "Parsing XML in C#"

  1. Trackback on Samantha Halfon on February 6, 2011 at 10:49 am

    New blog post: Parsing XML in C#: Lately, I dealt with webservices with C# while working on bluekiwi’s Microsoft… http://bit.ly/elXwnQ

Name (required)

Email (required)

Website

Leave a Reply

  • About Me


    photo of Samantha Halfon Samantha Halfon
    Software Engineer
    blueKiwi software
    Paris, France
    I enjoy playing with my computer(s), listening to Bob Dylan (and related artists) and watching movies (especially if they were directed by Martin Scorsese or John Cassavetes). Sometimes, I play a little guitar... If not doing any of the above, I am either riding a small red bike around Paris, or, making videos. About my videomaking please check out World Wide Angle and its blog.