You are here:   Articles
  |  Login

Programming Articles

Minimize
13

Asp.net provides many ways to parse Xml Documents. How you parse your Xml document depends on the structure of your document and what values you need to retrieve. This article gives some examples of how to retrieve values.

First, load your document into an XmlDocument.

XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(xmlstring);

For this article, I am using results returned from Ustream.tv as the xmlstring. That xml data looks like this.

<?xml version="1.0" encoding="UTF-8"?>
<xml>
<results>
<array key="0">
<id>4418588</id>
<title><![CDATA]Opening Remarks: State of the Internet Economy[]></title>
<protected>FALSE</protected>
<description><![CDATA]Opening Remarks by Lawrence E. Strickling, Assistant Secretary of Commerce for Communications and Information. []> </description>
<createdAt><![CDATA]2010-07-01 05:52:54[]></createdAt>
<rating>0.000</rating>
<lengthInSecond>604.537</lengthInSecond>
<totalViews>18</totalViews>
<url><![CDATA]http://www.ustream.tv/recorded/4418588[]></url>
<embedTag><![CDATA]<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444551240000" width="320" height="260" id="utv236367" name="utv_n_588994"> <param name="flashvars" value="autoplay=false" /> <param name="allowfullscreen" value="true" /> <param name="allowscriptaccess" value="always" /> <param name="src" value="http://www.ustream.tv/flash/video/4418588" /> <embed flashvars="autoplay=false" width="320" height="260" allowfullscreen="true" allowscriptaccess="always" id="utv902267" name="utv_n_588221" src="http://www.ustream.tv/flash/video/4418588" type="application/x-shockwave-flash" /></object>[]> </embedTag>
<liveHttpUrl> <![CDATA]http://ustream.vo.llnwd.net/pd4/0/1/8/80/88/1_588.mp4[]> </liveHttpUrl>
<imageUrl>
<small> <![CDATA]http://stat.ustream.tv/video/08/1_588_120x90_b_1:2.jpg[]> </small>
<medium> <![CDATA]http://stat.ustream.tv/video/08/1_588_320x240_b_1:2.jpg[]> </medium>
</imageUrl>
<sourceChannel>
<id>4343911</id>
<url><![CDATA]http://www.ustream.tv/channel/4343911[]></url>
</sourceChannel>
</array>
<array key="1">
data for the next video
</array>
</results>
<msg><![CDATA][]></msg>
<error><![CDATA][]></error>
<processTime>TRUE</processTime>
<version><![CDATA]mashery-r10[]></version>
</xml>

That is a mess. It is helpful if you write a program to display any xml document in a formatted way with name value pairs. That will give you a better look at the data so you can plan your attack. I have a program to display xml documents, but have not posted an article for it yet. Maybe one day.

With the data above, we want to extract information about each video. Each video is returned in an <array key="0">...</array> tag. We can use GetElementsByTagName method to return just the Array tags. That is easy.

XmlNodeList xnodelist = xdoc.GetElementsByTagName("array");

Now you need to go through each node in the list and get the information you want. Lets go to work on the first node in the list.

XmlNode xnode = xnodelist.Item(0);

You should put the above line in a loop and then call a method to extract each element.

You can get the value for the Key attribute on the Array tag from the Attributes property using the GetNamedItem method.

XmlNode xnodefound = xnode.Attributes.GetNamedItem(name).Value;

However, the Attribute might not be found, so we need to do some error checking. Here is a method you can use to extract attributes.

public virtual string Get_Attribute_Value(XmlNode xnode, string name)
{
if (string.IsNullOrEmpty(name)) return string.Empty;
XmlNode xnodefound = xnode.Attributes.GetNamedItem(name);
if (xnodefound == null) return string.Empty;
if (string.IsNullOrEmpty(xnodefound.Value)) return string.Empty;
return xnodefound.Value;
}

You can use the above to extract an attribute. You call the above method like this.

string key = Get_Attribute_Value(xnode, "key");

If you look back at the Xml data, you will see a number of child nodes with some of the tags: Description, Id, Title

The data for these tags is in the InnerText property. We can get these values by using the method SelectSingleNode. Here is the method with error checking.

public virtual string Get_Node_InnerText(XmlNode xnode, string name)
{
if (string.IsNullOrEmpty(name)) return string.Empty;
XmlNode xnodefound = xnode.SelectSingleNode(name);
if (xnodefound == null) return string.Empty;
string innertext = xnodefound.InnerText;
if (string.IsNullOrEmpty(innertext)) return string.Empty;
return innertext;
}

You can call this method similarly.

string desc = Get_Node_InnerText(xnode, "description");
string title = Get_Node_InnerText(xnode, "title");

Here is how it looks altogether. You still need to insert a loop to go through each element.

string xmlstring = "<you need to get the xml data>";
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml(xmlstring);
XmlNodeList xnodelist = xdoc.GetElementsByTagName("array");
//
// Insert the start of your loop here
// // Get a node //
XmlNode xnode = xnodelist.Item(0); // // Get the values for the node //
string key = Get_Attribute_Value(xnode, "key");
string desc = Get_Node_InnerText(xnode, "description");
string title = Get_Node_InnerText(xnode, "title"); //
// End your loop here
//

By Andrew Weitzen, Bronze Inc. (c) 2010

Bronze is the publisher of several online Internet journals including: InternetHandholding.com, DomainNames.gs, DotNetNuke.bz, Programmer.bz, Software.vg, WebHosting.vg

Posted in: Asp.net

Post Rating

Comments

Anonymous User
# Anonymous User
Thursday, September 16, 2010 5:42 PM
Ustream Live Video, Trust, Embed Facebook Video, Ebook Marketing

Post Comment

Only registered users may post comments.

Programmer Newsletter

Minimize

Subscribe to the Internet Handholding newsletter



Reccomend Programmer.bz

Minimize

Share/Bookmark Bookmark and Share