Hello,
I've been given a sample XML file in a structure that I haven't handled in VB.NET before (see image below), and would appreciate some guidance.
When I read the xml file in with:
Code:
Dim ds As DataSet = New DataSet("dataset")
ds.ReadXml("prod2.xml")
With DataGridView1
.DataSource = ds
.DataMember = ds.Tables(2).TableName
End With
I can see that I've got 3 x tables ... "Interface", "Product", "Value".
What I'm wondering is how do I get the "Value" table linked to "Product"? I was expecting (rightly or wrongly) to see some inherited values from product displayed in my DataGridView. Looking at the structure of the XML file, it seems to imply that there should be some inheritance???
Last edited by penguin5000; Jun 18th, 2008 at 05:39 AM.
At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
At work - VS 2008 Standard (VB)
.NET 2.0/3.5
When I loop through the DataSet tables to display each TableName, it confirms that it has been broken up into the tables I mentioned, and when I think about the way the company operates, it would make sense for them to provide a history of product value changes for each product (the XML file is a database product update file).
Last edited by penguin5000; Jun 19th, 2008 at 01:32 AM.
At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
At work - VS 2008 Standard (VB)
.NET 2.0/3.5
That may unnecessarily complicate things. You're better off using XPathNavigator or XmlDocument to read each product and its corresponding value , price, currency, etc.
Hi, I'm really struggling with this. Looking at MSDN, it seems to me that the XMLReader is perhaps the best one to use as I don't need to write back to the file.
The structure of the xml file seems very awkward to work with.
For example, I can get it to read each attribute of this line:
Dim reader As XmlReader = XmlReader.Create("prod2.xml")
reader.Read()
reader.MoveToFirstAttribute()
For x As Integer = 0 To reader.AttributeCount - 1
Console.WriteLine(reader.Name & " : " & reader.Item(x).ToString & vbCrLf)
reader.MoveToNextAttribute()
Next
But then it seems that I have to switch to some other way of continuing to read the file because the attributes aren't all along the same line.
It can't be this difficult, surely?
At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
At work - VS 2008 Standard (VB)
.NET 2.0/3.5
The XPathNavigator is also a cursor-like model for reading XML which is why I suggested it. You can use the XPathNavigator's Select() method to specify a node or attribute you want. Example:
The data set will have relationship object after you read in the xml, then you can grab the child value rows for each product row using the relationship:
Code:
Dim ds As New DataSet
ds.ReadXml("C:\testdata.xml")
For Each productRow As DataRow In ds.Tables("Product").Rows
Console.Write(productRow("productcode").ToString & " " & productRow("description").ToString)
For Each valueRow As DataRow In productRow.GetChildRows("Product_Value")
Console.Write(" " & valueRow("Price").ToString & " " & valueRow("currency").ToString)
Next
Console.WriteLine()
Next
Thank you very much for the code snippet. It is really appreciated. I've been battling with this all day, and was getting myself ready for a long night!
At home - VB.NET 2005/2008 Express, Visual Web Developer 2005 Express
At work - VS 2008 Standard (VB)
.NET 2.0/3.5