Check out System.Xml.Linq
Use the XDocument.Parse() method
...or the XDocument.Load() method
Imports System.IO
Imports System.Xml.Linq
Module Module1
Sub Main()
Dim xd As XDocument = XDocument.Load("c:\science\DaniWeb\DW_392100\DW_392100.xml")
For Each node In xd.Elements("Table").Nodes
Console.WriteLine(node)
Next
'fileIn.Close()
End Sub
End Module
thines01
Postaholic
2,425 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
Since your values are embedded in attributes, a different technique (that usual) will need to be used.
Here is something better:
Imports System.Linq
Imports System.Xml.Linq
Module Module1
Function GetProdById(ByRef xd As XDocument, ByVal strId As String) As XElement
Return _
(From p In xd.Descendants("Table").Descendants("Product")
Where (p.Element("Product_id").Attributes("value").ElementAt(0).Value.Equals(strId))
Select p).FirstOrDefault()
End Function
Function GetNumElements(ByRef xd As XDocument) As Integer
Return _
(From prod In xd.Descendants("Table")
Select prod.Elements().Count()).First()
End Function
Sub Main()
Dim xd As XDocument = XDocument.Load("c:\science\DaniWeb\DW_392100\DW_392100.xml")
Console.WriteLine(GetNumElements(xd))
Dim xeProd2 As XElement = GetProdById(xd, "2")
Dim xeProd1 As XElement = GetProdById(xd, "1")
Console.WriteLine(xeProd2.Element("Product_price").Attribute("value").Value)
End Sub
End Module
If you can re-structure the XML, it would be easier than this.
thines01
Postaholic
2,425 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
If you could re-structure your XML to this:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Table>
<Product>
<id>1</id>
<description>Product 1</description>
<price>1000</price>
</Product>
<Product>
<id>2</id>
<description>Product 2</description>
<price>5000</price>
</Product>
</Table>
...you could use code like this:
Imports System.Linq
Imports System.Xml.Linq
Module Module1
Function GetProdById(ByRef xd As XDocument, ByVal strId As String) As XElement
Return _
(From p In xd.Descendants("Table").Descendants("Product")
Where p.Element("id").Value.Equals(strId)
Select p).FirstOrDefault()
End Function
Function GetNumElements(ByRef xd As XDocument) As Integer
Return _
(From prod In xd.Descendants("Table")
Select prod.Elements().Count()).First()
End Function
Sub Main()
Dim xd As XDocument = XDocument.Load("c:\science\DaniWeb\DW_392100\DW_392100-2.xml")
Console.WriteLine(GetNumElements(xd))
Dim xeProd2 As XElement = GetProdById(xd, "2")
Dim xeProd1 As XElement = GetProdById(xd, "1")
Console.WriteLine(xeProd1.Element("price").Value)
Console.WriteLine(xeProd2.Element("price").Value)
End Sub
End Module
thines01
Postaholic
2,425 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402
thines01
Postaholic
2,425 posts since Oct 2009
Reputation Points: 445
Solved Threads: 402