首页 > 解决方案 > 使用 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

如果有人可以提供帮助,那就太好了,但即使是朝着正确的方向轻推,也将不胜感激

标签: xmlvb.net

解决方案


哇!很难看出两者之间的关系。这是一段可能让您入门的代码。该片段包含您应该阅读的两个链接。

'
'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

推荐阅读