在VB.NET中解析xml文件时出现问题 [英] Problem parsing xml file in VB.NET

查看:108
本文介绍了在VB.NET中解析xml文件时出现问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我是VB.NET和xml的新手,我试图解析下面提到的格式的xml文件,并在VB.NET中使用文本框显示它.但是我无法解析它.任何人都可以给我一个代码片段来解析如下所示的xml:

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>

推荐答案

借助VB.Net,您可以使用^ ]解析XML文件.对于您的示例,您只需要执行以下操作:-

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

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


这篇关于在VB.NET中解析xml文件时出现问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆