Simple XML parsing question..

  • Thread starter Thread starter Ivan Demkovitch
  • Start date Start date
I

Ivan Demkovitch

Hi!

I'm trying to parse RSS news feed with code as simple as possible.

Simple XML:
<?xml version="1.0"?>
<rss version="2.0">
<channel>

<item>
<title>The situation of F1 in Russia</title>
<link>http://www.grandprix.com/ns/ns12094.html</link>
<description>R-Fast, the Russian Federation of Autosport and Tourism, the
national sporting authority of Russia, has been giving details of its plans
to increase motor racing activities in Russia. These date back to 1998 when
R-FAST began working with the Moscow City Government on the idea of an
international racing circuit in the city. </description>
<guid>http://www.grandprix.com/ns/ns12094.html</guid>
<pubDate>Tue, 28 Oct 2003 8:36:40 GMT</pubDate>
</item>

<item>
<title>Massa at Sauber - it's official</title>
<link>http://www.grandprix.com/ns/ns12096.html</link>
<description>The long-awaited announcement from Sauber that Felipe Massa
will be driving of the team in 2004 is no surprise. Massa has agreed a
two-year deal with the Swiss team but it is expected that he will have a
get-out clause to move to Ferrari in 2005 if a drive is
available.</description>
<guid>http://www.grandprix.com/ns/ns12096.html</guid>
<pubDate>Tue, 28 Oct 2003 14:43:20 GMT</pubDate>
</item>

</channel>
</rss>



Now I'm writing code:

void XMLParse(string xmldata) {

//using Microsoft XML Parser
XmlTextReader xml_read = new XmlTextReader(new
StringReader(xmldata));

xml_read.WhitespaceHandling = WhitespaceHandling.None;

//Get all Items into node list...
XmlNodeList items = oXML.GetElementsByTagName("item");

foreach(XmlNode node in items){

//What's here???
}




QUESTION??

When in foreach I need simply get values of title, link, guid and pubDate

How do I do it?
 
Ivan,

For each node in the foreach statement ("node"), you can call the
SelectSingleNode method and pass the name of the decendant element, like so:

XmlNode pobjTitleNode = node.SelectSingleNode("title");
XmlNode pobjLinkNode = node.SelectSingleNode("link");

And so on. From there, you can use the properties of the node to get
the text.

Hope this helps.
 
Actually is was simpler (just got it)
Let me know if this could present problems:

node.SelectSingleNode("title").InnerText

returned what I needed saving some lines of code...


Nicholas Paldino said:
Ivan,

For each node in the foreach statement ("node"), you can call the
SelectSingleNode method and pass the name of the decendant element, like so:

XmlNode pobjTitleNode = node.SelectSingleNode("title");
XmlNode pobjLinkNode = node.SelectSingleNode("link");

And so on. From there, you can use the properties of the node to get
the text.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Ivan Demkovitch said:
Hi!

I'm trying to parse RSS news feed with code as simple as possible.

Simple XML:
<?xml version="1.0"?>
<rss version="2.0">
<channel>

<item>
<title>The situation of F1 in Russia</title>
<link>http://www.grandprix.com/ns/ns12094.html</link>
<description>R-Fast, the Russian Federation of Autosport and Tourism, the
national sporting authority of Russia, has been giving details of its plans
to increase motor racing activities in Russia. These date back to 1998 when
R-FAST began working with the Moscow City Government on the idea of an
international racing circuit in the city. </description>
<guid>http://www.grandprix.com/ns/ns12094.html</guid>
<pubDate>Tue, 28 Oct 2003 8:36:40 GMT</pubDate>
</item>

<item>
<title>Massa at Sauber - it's official</title>
<link>http://www.grandprix.com/ns/ns12096.html</link>
<description>The long-awaited announcement from Sauber that Felipe Massa
will be driving of the team in 2004 is no surprise. Massa has agreed a
two-year deal with the Swiss team but it is expected that he will have a
get-out clause to move to Ferrari in 2005 if a drive is
available.</description>
<guid>http://www.grandprix.com/ns/ns12096.html</guid>
<pubDate>Tue, 28 Oct 2003 14:43:20 GMT</pubDate>
</item>

</channel>
</rss>



Now I'm writing code:

void XMLParse(string xmldata) {

//using Microsoft XML Parser
XmlTextReader xml_read = new XmlTextReader(new
StringReader(xmldata));

xml_read.WhitespaceHandling = WhitespaceHandling.None;

//Get all Items into node list...
XmlNodeList items = oXML.GetElementsByTagName("item");

foreach(XmlNode node in items){

//What's here???
}




QUESTION??

When in foreach I need simply get values of title, link, guid and pubDate

How do I do it?
 
Back
Top