Click here to Skip to main content
15,903,744 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
<?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>
Posted

1 solution

With VB.Net you can use XML literals[^] to parse XML files. For your example you just need to do something like this:-

VB
Class Form1

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'can use XDocument.Load(fileName) to load from file
        Dim xDoc As XDocument = <?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>
        Dim productList As New List(Of Product)
        For Each product In From element In xDoc...<Product>
            Dim selectdProduct As New Product
            selectdProduct.ProductID = Convert.ToInt32(product...<Product_id>.@value)
            selectdProduct.ProductName = product...<Product_name>.@value
            selectdProduct.ProductPrice = Convert.ToDecimal(product...<Product_price>.@value)
            productList.Add(selectdProduct)
        Next

    End Sub
End Class

Class Product

    Public ProductID As Integer
    Public ProductName As String
    Public ProductPrice As Decimal

End Class


Hope this helps
 
Share this answer
 
Comments
Gani_2 7-Nov-11 7:22am    
Thank you very much for the quick answer. I used the above code and tried displaying the list but couldn't see any values getting displayed.

Is there a way to be done using the XmlDocument class?
Gani_2 7-Nov-11 7:59am    
I am sorry for the above comment.

I am able to see the values in the list. Thanks.

But, is there a way to do the same thing using XmlDocument class?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900