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>.

Recommended Answers

All 4 Replies

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

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.

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

Did this help?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.