XML节点读取

时间:2019-12-18 17:42:22

标签: xml vb.net

我有一个xml文件,如下所示;

<ZPPORDER01>
  <IDOC BEGIN="1">
    <EDI_DC40 SEGMENT="1">
      <TABNAM>EDI_DC40</TABNAM>
      <MANDT>100</MANDT>
      <DOCNUM>0000000000000001</DOCNUM>
    </EDI_DC40>
    <Z1PPORDITEM SEGMENT="1">
      <AUFNR>000000000123</AUFNR>
      <POSNR>0001</POSNR>
      <Z1PPORDOPER SEGMENT="1">
        <VORNR>0010</VORNR>
        <ARBPL>PIGME</ARBPL>
        <Z1PPORDCOMP SEGMENT="1">
          <POSNR>0100</POSNR>
          <CPARAM>RV ;</CPARAM>
        </Z1PPORDCOMP>
        <Z1PPORDCOMP SEGMENT="1">
          <POSNR>0200</POSNR>
          <CPARAM>PLT;</CPARAM>
        </Z1PPORDCOMP>
     </Z1PPORDOPER>
    </Z1PPORDITEM>
  </IDOC>
</ZPPORDER01>

我想为每个<Z1PPORDCOMP SEGMENT="1">节点读取PosNR和CPARAM。如图所示,有多个具有相同节点名称(Z1PPORDCOMP SEGMENT =“ 1”)和相同子名称(POSNR,CPARAM)的节点。

我想阅读孩子所有的内心文字,并在扫描中分配给4个不同的字符串。

我写了一个脚本...我定义了4个字符串来读取每个孩子的数据。我可以从前两个值中读取值,但是我不知道如何从下一个子值中获取数据。

我已经搜索过Xml。使用了XPath,但在这种情况下我不知道如何使用它。


dim doc as System.Xml.XmlDocument;
dim node as System.Xml.XmlNode;
doc = new System.Xml.XmlDocument;
doc.Load("\\mypc\ShareOn\INPUT\Test.xml");

dim PosNR0100 as string ;
dim PosNR0100_CPARAM as string ;
dim PosNR0200 as string ;
dimPosNR0200_CPARAM as string ;

PosNR0100       = doc.SelectSingleNode("/ZPPORDER01/IDOC [@BEGIN='1']/Z1PPORDITEM [@SEGMENT='1']/Z1PPORDOPER [@SEGMENT='1']/Z1PPORDCOMP [@SEGMENT='1']/POSNR").InnerText;

PosNR0100_CPARAM    = doc.SelectSingleNode("/ZPPORDER01/IDOC [@BEGIN='1']/Z1PPORDITEM [@SEGMENT='1']/Z1PPORDOPER [@SEGMENT='1']/Z1PPORDCOMP [@SEGMENT='1']/CPARAM").InnerText;
PosNR0200       = ?
PosNR0200_CPARAM    = ?

2 个答案:

答案 0 :(得分:1)

使用XML序列化

创建代表您的数据的类

Imports System.Xml.Serialization
Imports System.IO
Public Class ZPPORDER01
    Public Property IDOC As IDOC
End Class
Public Class IDOC
    Public Property EDI_DC40 As EDI_DC40
    Public Property Z1PPORDITEM As Z1PPORDITEM
End Class

Public Class EDI_DC40
    Public Property TABNAM As String
    Public Property MANDT As String
    Public Property DOCNUM As String
End Class
Public Class Z1PPORDITEM
    <XmlAttribute>
    Public Property SEGMENT As Integer
    Public Property AUFNR As String
    Public Property POSNR As String
    Public Property Z1PPORDOPER As Z1PPORDOPER
End Class
Public Class Z1PPORDOPER
    <XmlAttribute>
    Public Property SEGMENT As Integer
    Public Property VORNR As String
    Public Property ARBPL As String
    <XmlElement("Z1PPORDCOMP")>
    Public Property Z1PPORDCOMPs As List(Of Z1PPORDCOMP)
End Class
Public Class Z1PPORDCOMP
    <XmlAttribute>
    Public Property SEGMENT As Integer
    Public Property POSNR As String
    Public Property CPARAM As String
End Class

然后反序列化

Dim s As New XmlSerializer(GetType(ZPPORDER01))
Dim z As ZPPORDER01
Using sr As New StreamReader("filename.xml")
    z = DirectCast(s.Deserialize(sr), ZPPORDER01)
End Using
For Each comp In z.IDOC.Z1PPORDITEM.Z1PPORDOPER.Z1PPORDCOMPs
    MessageBox.Show($"POSNR: {comp.POSNR}, CPARAM: {comp.CPARAM}")
Next

答案 1 :(得分:0)

您可以像这样使用xpath查询:

Imports System.Xml

Module Module1

    Sub Main()
        Dim src = "C:\temp\ZPPORDER.xml"
        Dim doc As New XmlDocument
        doc.Load(src)

        Dim posnrs As New List(Of String)
        Dim cparams As New List(Of String)

        Dim z1ppordcomps = doc.SelectNodes("//Z1PPORDCOMP[@SEGMENT='1']")
        For Each n As XmlNode In z1ppordcomps
            Dim posnr = n.SelectSingleNode("POSNR")
            Dim cparam = n.SelectSingleNode("CPARAM")
            If posnr IsNot Nothing AndAlso cparam IsNot Nothing Then
                posnrs.Add(posnr.InnerText)
                cparams.Add(cparam.InnerText)
            End If
        Next

        Console.WriteLine("POSNR: " & String.Join(",", posnrs))
        Console.WriteLine("CPARAM: " & String.Join(", ", cparams))

        Console.ReadLine()

    End Sub

End Module

输出:

  

POSNR:0100,0200
  CPARAM:RV;,PLT;

您可以分别使用posnrs(0)posnrs(1)cparams(0)cparams(1)来访问它们。