Simple XML parsing question..

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?
 
N

Nicholas Paldino [.NET/C# MVP]

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.
 
I

Ivan Demkovitch

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Read XML file into GridView 2
Parsing RSS (XML) 4
XPath issue 2
showing date on ashx page (rss) 4
Need an explanation rss xml 4
How to use rss 7
RSS feed XML linq 2
How to solve this? The headache is growing. 2

Top