How to solve this? The headache is growing.

S

Shapper

Hello,

I have a XML file (RSS 2.0 format). Something as follows:

<?xml version="1.0"?>
<rss version="2.0">
<channel>
<title>News</title>
<description>Latest News</description>
<item>
<title>Title</title>
<link>Url</link>
<description>Description</description>
<pubDate>Fri, 20 May 2005 12:30:00 GMT</pubDate>
</item>
...

I need to do the following:

1. Load the 10 most recent items (using pubDate as reference).

2. Place the loaded items in a dataset.

3. pubDate must be a valid ASP.Net date/time format.
I think the standard XML pubDate format is not:
Fri, 20 May 2005 12:30:00 GMT

Well, at least I am getting a string in my dataset [pubDate] field.

I am having huge problems with (1) and (3).

Here is my code:

Dim news As New XmlDocument()
news.Load("http://www.domain.com/news.rss")
Dim x As Integer
Dim xNodeList As XmlNodeList =
news.SelectNodes("/rss[@version='2.0']/channel/item")
Dim dsNews As DataSet = New DataSet()
Dim xReader As XmlTextReader
For x = 0 To xNodeList.Count-1
xReader = New XmlTextReader(xNodeList.item(x).OuterXml,
XmlNodeType.Element, new XmlParserContext(Nothing, Nothing, Nothing,
XmlSpace.None))
dsNews.ReadXml(xReader, XmlReadMode.InferSchema)
Next x

Could someone help me with this?

It's making me crazy. I have been looking for the solution for past 2
days.

Thank You,
Miguel
 
R

Ryan Ternier

To my knowledge you can't use xPath with an XML text reader.
XPath only works with XmlDocument.

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(XML_FILE_HERE);


string xPath = "/rss[@version='2.0']/channel/item";
XmlElement xmlRoot = xmlDoc.DocumentElement;
XmlNodeList nodes = xmlRoot.SelectNodes(xPath);

foreach(XmlNode node in nodes)
{
ListBox1.Items.Add(new ListItem("Item - " + node["title"]));
}

/RT
 
S

Shapper

Hi Ryan,

Now I got lost. The code I sent you is working.
In fact all item data is loaded, binded to the dataset and later
displayed in a datagrid.

The problems are:
1. My pubDate is returned as string and not as Date/Time format.
2. I don't know how to Load only the last 10 items ordered by pubDate.

I don't know if to get this I need to make some changes or just recreate
the code in other way.

Any suggestion?

I am not very good in XML so some help would be appreciated.

Thanks,
Miguel



To my knowledge you can't use xPath with an XML text reader.
XPath only works with XmlDocument.

XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(XML_FILE_HERE);


string xPath = "/rss[@version='2.0']/channel/item";
XmlElement xmlRoot = xmlDoc.DocumentElement;
XmlNodeList nodes = xmlRoot.SelectNodes(xPath);

foreach(XmlNode node in nodes)
{
ListBox1.Items.Add(new ListItem("Item - " + node["title"]));
}

/RT
Hello,

I have a XML file (RSS 2.0 format). Something as follows:

<?xml version="1.0"?>
<rss version="2.0">
<channel>
<title>News</title>
<description>Latest News</description>
<item>
<title>Title</title>
<link>Url</link>
<description>Description</description>
<pubDate>Fri, 20 May 2005 12:30:00 GMT</pubDate>
</item>
...

I need to do the following:

1. Load the 10 most recent items (using pubDate as reference).

2. Place the loaded items in a dataset.

3. pubDate must be a valid ASP.Net date/time format.
I think the standard XML pubDate format is not:
Fri, 20 May 2005 12:30:00 GMT

Well, at least I am getting a string in my dataset [pubDate] field.

I am having huge problems with (1) and (3).

Here is my code:

Dim news As New XmlDocument()
news.Load("http://www.domain.com/news.rss")
Dim x As Integer
Dim xNodeList As XmlNodeList =
news.SelectNodes("/rss[@version='2.0']/channel/item")
Dim dsNews As DataSet = New DataSet()
Dim xReader As XmlTextReader
For x = 0 To xNodeList.Count-1
xReader = New XmlTextReader(xNodeList.item(x).OuterXml,
XmlNodeType.Element, new XmlParserContext(Nothing, Nothing, Nothing,
XmlSpace.None))
dsNews.ReadXml(xReader, XmlReadMode.InferSchema)
Next x

Could someone help me with this?

It's making me crazy. I have been looking for the solution for past 2 days.

Thank You,
Miguel
 

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

Members online

Forum statistics

Threads
473,743
Messages
2,569,477
Members
44,898
Latest member
BlairH7607

Latest Threads

Top