Hi,

I am new to VB.NET and xml and I am trying to parse the xml file of format mentioned below and display it using textbox in VB.NET. But I am not able to parse it. Can anybody pls give me a code snippet to parse the xml shown below :

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Table>
  <Product>
    <Product_id value="1"/>
    <Product_name value="Product 1"/>
    <Product_price value="1000"/>
  </Product>
  <Product>
    <Product_id value="2"/>
    <Product_name value="Product 2"/>
    <Product_price value="5000"/>
  </Product>
</Table>.

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

Edited 5 Years Ago by thines01: added code

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.

Edited 5 Years Ago by thines01: clarity

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
This article has been dead for over six months. Start a new discussion instead.