gpt4 book ai didi

xml - 如何使用 VB .net 解析 XML

转载 作者:行者123 更新时间:2023-12-04 13:48:35 25 4
gpt4 key购买 nike

我有一个 XML 文件,我想将其转换为一个类以在 VB 网络中使用。

<?xml version="1.0" encoding="windows-1252"?>
<MatML_Doc>
<Material>
<BulkDetails>
<Name>23133385</Name>
<Class>
<Name>1 - Carbon Steel</Name>
</Class>
<Source source=""/>
<PropertyData property="Material Type">
<Data format="string">IsotropicMaterial</Data>
</PropertyData>
<PropertyData property="Mass Density (RHO)_1">
<Data format="exponential">7.87e-6</Data>
</PropertyData>
<PropertyData property="Spec Organization">
<Data format="string">SAE</Data>
</PropertyData>
<PropertyData property="Spec Name">
<Data format="string">J1199</Data>
</PropertyData>
<PropertyData property="Spec Grade">
<Data format="string">CLASS 9.8</Data>
</PropertyData>
</BulkDetails>
</Material>
<Material>
<BulkDetails>
<Name>23133419</Name>
<Class>
<Name>1 - Carbon Steel</Name>
</Class>
<Source source=""/>
<PropertyData property="Material Type">
<Data format="string">IsotropicMaterial</Data>
</PropertyData>
<PropertyData property="Mass Density (RHO)_1">
<Data format="exponential">7.87e-6</Data>
</PropertyData>
<PropertyData property="Spec Organization">
<Data format="string">EN</Data>
</PropertyData>
<PropertyData property="Spec Name">
<Data format="string">10130</Data>
</PropertyData>
<PropertyData property="Spec Grade">
<Data format="string">DC05</Data>
</PropertyData>
</BulkDetails>
</Material>
...

我正在尝试将其转换为自定义类的列表:
Public Class MyMaterial
Public Name As String
Public Class As String
Public Org As String
Public Spec As String
Public Grade As String
Public Density As Double
End Class

我不知道如何遍历 xml 以创建一个列表(MyMaterial)。目前,我正在使用 stringreader 一次通过一行运行条件检查以创建此列表。必须有一种更有效的方法来使用 vb.net xml 工具。

解决方案

使用下面的帮助,这是对我有用的解决方案。
Dim doc As XDocument = XDocument.Load(XMLLocation)
Dim XMLMaterials As IEnumerable(Of XElement) = doc.Root.Elements("Material")
For Each XEL1 As XElement In XMLMaterials
Dim material As New MyMaterial
material.Name = XEL1.Element("BulkDetails").Element("Name").Value
material.Category = XEL1.Element("BulkDetails").Element("Class").Element("Name").Value
For Each XEL2 As XElement In XEL1.Element("BulkDetails").Elements.Where(Function(d) d.Name = "PropertyData")
If XEL2.Attribute("property").Value = "Mass Density (RHO)_1" Then
material.Density = XEL2.Value
ElseIf XEL2.Attribute("property").Value = "Spec Organization" Then
material.Org = XEL2.Value
ElseIf XEL2.Attribute("property").Value = "Spec Name" Then
material.Spec = XEL2.Value
ElseIf XEL2.Attribute("property").Value = "Spec Grade" Then
material.Grade = XEL2.Value
End If
Next
MaterialsList.Add(material)
If Not CatagoryNames.Contains(material.Category) Then CatagoryNames.Add(material.Category)
If Not Organizations.Contains(material.Org) Then Organizations.Add(material.Org)
Next

最佳答案

您可以使用 linq to xml 来读取和操作 xml。为了遍历所有元素,您应该使用递归代码。

Private Sub ReadXml()
Dim xmlAll = <?xml version="1.0" encoding="windows-1252"?>
<MatML_Doc>
<Material>
<BulkDetails>
<Name>23133385</Name>
<Class>
<Name>1 - Carbon Steel</Name>
</Class>
<Source source=""/>
<PropertyData property="Material Type">
<Data format="string">IsotropicMaterial</Data>
</PropertyData>
<PropertyData Property="Mass Density (RHO)_1">
<Data format="exponential">7.87e-6</Data>
</PropertyData>
<PropertyData Property="Spec Organization">
<Data format="string">SAE</Data>
</PropertyData>
<PropertyData Property="Spec Name">
<Data format="string">J1199</Data>
</PropertyData>
<PropertyData Property="Spec Grade">
<Data format="string">CLASS 9.8</Data>
</PropertyData>
</BulkDetails>
</Material>
<Material>
<BulkDetails>
<Name>23133419</Name>
<Class>
<Name>1 - Carbon Steel</Name>
</Class>
<Source source=""/>
<PropertyData property="Material Type">
<Data format="string">IsotropicMaterial</Data>
</PropertyData>
<PropertyData Property="Mass Density (RHO)_1">
<Data format="exponential">7.87e-6</Data>
</PropertyData>
<PropertyData Property="Spec Organization">
<Data format="string">EN</Data>
</PropertyData>
<PropertyData Property="Spec Name">
<Data format="string">10130</Data>
</PropertyData>
<PropertyData Property="Spec Grade">
<Data format="string">DC05</Data>
</PropertyData>
</BulkDetails>
</Material>
</MatML_Doc>

For Each xel In xmlAll.Elements
Dim material As New MyMaterial
material.Name = xel.Element("BulkDetails").Element("Name").Value
material.Classe = xel.Element("BulkDetails").Element("Classe").Element("Name").Value
material.Org = xel.Element("BulkDetails").Elements.Where(Function(d) d.Name = "PropertyData" And d.Attribute("property").Value = "Material Type").Value
'Ect
Next

End Sub

还有将 Xml 序列化为类的序列化库。

关于xml - 如何使用 VB .net 解析 XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37460165/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com