xml - 使用 VB.NET 读取 XML 文件
问题描述
我需要一些关于阅读 XML 文件的方向
我的任务是获取以下 XML 文件(为此缩短版本)
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<CompositeDoors>
<CompositeDoor
AddOnLeft1="[] Eurocell door 15mm"
AddOnLeft1_Code="b95e1973f58e747b3e4f7d251097fe9d">
<Door
CylinderType="[ZL30-30NAS] base Standard Satin Chrome Cylinder [Nickel]"
CylinderType_Code="112b68d790bc50a835e1c1b1589e57a0"
/>
</CompositeDoor>
<Sidelights>
<Sidelight
Coupler="Eurocell window Coupler"
Coupler_Code="03f7f14d5d3484f61fab9fd15239a3e0"
/>
<Sidelight
ArgonGas="Argon Gas"
ArgonGas_Code="No"
Coupler="Eurocell window Coupler"
Coupler_Code="03f7f14d5d3484f61fab9fd15239a3e0"
/>
</Sidelights>
<Topbox
ArgonGas="Argon Gas"
ArgonGas_Code="No"
Glazed="Yes"
TopboxDrop="380"
/>
</CompositeDoors>
并将其转换为另一个程序可以读取的 xml 文件(类似这样)
<?xml version="1.0" encoding="utf-8" ?>
<order>
<Header>
<customer_code>3080</customer_code>
<del._Name></del._Name>
<Del._Address_1>Unit 1, </Del._Address_1>
</Header>
<Frame_1>
<Quantity>1</Quantity>
<Overall_Width>950</Overall_Width>
<Overall_Height>2050</Overall_Height>
</Frame_1>
<Frame_2>
<Quantity>1</Quantity>
<Overall_Width>950</Overall_Width>
<Overall_Height>2050</Overall_Height>
</Frame_2>
<Frame_3>
<Quantity>1</Quantity>
<Overall_Width>950</Overall_Width>
<Overall_Height>2050</Overall_Height>
</Frame_3>
<Frame_4>
<Quantity>1</Quantity>
<Overall_Width>950</Overall_Width>
<Overall_Height>2050</Overall_Height>
</Frame_4>
</order>
我正在尝试使用此代码循环浏览节点以查找相关部分,但我正在努力
While reader.Read()
' Check for start elements. '
If reader.IsStartElement() Then
If reader.Name = "CompositeDoors" Then
ElseIf reader.Name = "CompositeDoor" Then
lbHD_customer_code.InnerText = reader("CustomerAccountCode")
ElseIf reader.Name = "Sidelight" Then
For Each m_node In m_nodelist
framenumber = framenumber + 1
If framenumber = 1 Then
如果有人可以提供帮助,那就太好了,但即使是朝着正确的方向轻推,也将不胜感激
解决方案
哇!很难看出两者之间的关系。这是一段可能让您入门的代码。该片段包含您应该阅读的两个链接。
'
'read about
'XElement Class
' https://docs.microsoft.com/en-us/dotnet/api/system.xml.linq.xelement?view=netframework-4.8
'Xml literals
' https://docs.microsoft.com/en-us/dotnet/visual-basic/language-reference/xml-literals/xml-element-literal
'
Dim inxe As XElement
'to load from file
' inxe = XElement.Load("path to file")
'for testing
inxe = <CompositeDoors>
<CompositeDoor
AddOnLeft1="[] Eurocell door 15mm"
AddOnLeft1_Code="b95e1973f58e747b3e4f7d251097fe9d">
<Door
CylinderType="[ZL30-30NAS] base Standard Satin Chrome Cylinder [Nickel]"
CylinderType_Code="112b68d790bc50a835e1c1b1589e57a0"
/>
</CompositeDoor>
<Sidelights>
<Sidelight
Coupler="Eurocell window Coupler"
Coupler_Code="03f7f14d5d3484f61fab9fd15239a3e0"
/>
<Sidelight
ArgonGas="Argon Gas"
ArgonGas_Code="No"
Coupler="Eurocell window Coupler"
Coupler_Code="FOOOOOOOOO03f7f14d5d3484f61fab9fd15239a3e0"
/>
</Sidelights>
<Topbox
ArgonGas="Argon Gas"
ArgonGas_Code="No"
Glazed="Yes"
TopboxDrop="380"
/>
</CompositeDoors>
Stop 'step through the code
Dim framePROTO As XElement = <Frame id=""></Frame>
Dim frameID As Integer = 1
Dim outxe As XElement = <order></order>
For Each el As XElement In inxe...<Sidelight>
Dim newFrame As New XElement(framePROTO)
newFrame.@id = frameID.ToString
frameID += 1
Dim itm As XElement = <itm><%= el.@Coupler %></itm>
itm.@code = el.@Coupler_Code
newFrame.Add(itm)
outxe.Add(newFrame)
Next
Stop ' examine outxe
推荐阅读
- ruby - chromedriver 不使用 download.defaultDirectory
- docker - 如何在启用 ssh 的 docker 节点上运行 Kubernetes?
- node.js - Node-js:处理多个文件与使用行偏移处理大文件
- android - API 请求 | OkHttp | 通过带有两个对象的 okhttp 的 api 请求
- java - Java 8 使用一个接口在另一个接口中实现方法
- r - 无法将 ggplot2 加载到 Mac OS X 的 R 中
- python - imaplib 用于同步 Outlook Office 365 电子邮件是否安全?
- python - 通过变量计算 DateTimeField()
- c# - MVC5-Signal R-Redis 没有收到所有消息
- amazon-web-services - 将数据从 S3 传输到 AWS 中的 Postgres RDS