Why do I allways get the same values?

S

Shapper

Hello,

I am reading the <item> nodes from a XML file and add them to a dataset.

When I display my dataset in a datagrid I always see the same record.
It's like the For loop is not moving from one item to the next one.

This is my code:

' Load RSS File
Dim news As New XmlDocument()
news.Load("http://www.domain.com/_rss/news.rss")

' Create Dataset
Dim dsNews As DataSet = New DataSet()
dsNews.Tables.Add
dsNews.Tables(0).Columns.Add("title", GetType(String))
dsNews.Tables(0).Columns.Add("description", GetType(String))

' Read Item Nodes
Dim items As XmlNodeList =
news.SelectNodes("/rss[@version='2.0']/channel/item")

For Each item As XmlNode In items

Dim row As DataRow = dsNews.Tables(0).NewRow()
row.Item("title") =
CType(news.SelectSingleNode("/rss[@version='2.0']/channel/item/title").InnerText,
String)
row.Item("description") =
CType(news.SelectSingleNode("/rss[@version='2.0']/channel/item/description").InnerText,
String)
dsNews.Tables(0).Rows.Add(row)

Next

Can someone tell me what is wrong?

Thank You,
Miguel
 
C

Craig Deelsnyder

Shapper said:
Hello,

I am reading the <item> nodes from a XML file and add them to a dataset.

When I display my dataset in a datagrid I always see the same record.
It's like the For loop is not moving from one item to the next one.

This is my code:

' Load RSS File
Dim news As New XmlDocument()
news.Load("http://www.domain.com/_rss/news.rss")

' Create Dataset
Dim dsNews As DataSet = New DataSet()
dsNews.Tables.Add
dsNews.Tables(0).Columns.Add("title", GetType(String))
dsNews.Tables(0).Columns.Add("description", GetType(String))

' Read Item Nodes
Dim items As XmlNodeList =
news.SelectNodes("/rss[@version='2.0']/channel/item")

For Each item As XmlNode In items

Dim row As DataRow = dsNews.Tables(0).NewRow()
row.Item("title") =
CType(news.SelectSingleNode("/rss[@version='2.0']/channel/item/title").InnerText,
String)
row.Item("description") =
CType(news.SelectSingleNode("/rss[@version='2.0']/channel/item/description").InnerText,
String)
dsNews.Tables(0).Rows.Add(row)

Next

Can someone tell me what is wrong?

Thank You,
Miguel

It's because you're doing the xpath query again inside of the for...each
loop. Once you have are looking at one of the item nodes inside the
loop, just look at its children (or do an xpath query from item, not
from news). something like:

item.SelectSingleNode("/description").InnerText
 

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,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top