且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在VB.NET中解析xml文件时出现问题

更新时间:2022-12-03 21:31:49

借助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