2 Replies - 1424 Views - Last Post: 07 November 2011 - 11:05 AM Rate Topic: -----

#1 gani_2  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 07-November 11

Parsing xml file of different format using VB.NET

Posted 07 November 2011 - 08:29 AM

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>




I am able to parse the data of xml file of below mentioned format using XmlDocument class methods. But same doesn't work with above format. Pls suggest.

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Table>
  <Product>
    <Product_id>1</Product_id>
    <Product_name>Product 1</Product_name>
    <Product_price>1000</Product_price>
  </Product>
  <Product>
    <Product_id>2</Product_id>
    <Product_name>Product 2</Product_name>
    <Product_price>2000</Product_price>
  </Product>
  <Product>
    <Product_id>3</Product_id>
    <Product_name>Product 3</Product_name>
    <Product_price>3000</Product_price>
  </Product>
  <Product>
    <Product_id>4</Product_id>
    <Product_name>Product 4</Product_name>
    <Product_price>4000</Product_price>
  </Product>
</Table>




Is This A Good Question/Topic? 0
  • +

Replies To: Parsing xml file of different format using VB.NET

#2 Tryparse  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 80
  • View blog
  • Posts: 193
  • Joined: 20-April 10

Re: Parsing xml file of different format using VB.NET

Posted 07 November 2011 - 08:47 AM

It's difficult to know where the problem lies without seeing some code. However, I can say that the primary difference between the 2 formats is that the bottom one stores the data in the InnerText of the nodes, while the upper one uses Attributes. To illustrate the difference between working with the 2, I'll post some sample xml & code from a little filesystem monitor:
<Directories Enable="TRUE">
    <Path subs="FALSE" milliseconds="30000">C:\Projects\Files\</Path>
    <Path subs="FALSE" milliseconds="30000">C:\Scripts\</Path>
</Directories>

'As we see above, each Path node contains both Attributes and InnerText.
'Here's a loop that processes the data
If Boolean.Parse(xdoc.GetElementsByTagName("Directories")(0).Attributes("Enable").Value) Then

    For Each node As XmlNode In xdoc.GetElementsByTagName("Directories")(0) 'get the directories to monitor and their intervals
        strName = node.InnerText 'get the path to the folder
        intInterval = Integer.Parse(node.Attributes("milliseconds").Value) 'get the attribute's value
        '...
    Next

End If


That said, another option you might want to think about for your xml is to have all of the information for a product contained in the attributes of a single node. It results in much less xml:
<Product id="1" name="Product 1" price="1000"/>
<Product id="2" name="Product 2" price="5000"/>

This post has been edited by Tryparse: 07 November 2011 - 08:52 AM

Was This Post Helpful? 0
  • +
  • -

#3 _HAWK_  Icon User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1043
  • View blog
  • Posts: 4,054
  • Joined: 02-July 08

Re: Parsing xml file of different format using VB.NET

Posted 07 November 2011 - 11:05 AM

That format lends itself to loading into a dataset. Dataset.ReadXml(filename), then load a datagridview with the data by setting it's datasource property with the dataset.Table (0). You can also bind some controls to this dataset too.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1