Vb.Net 读取并从 XML 中提取数据

huangapple go评论84阅读模式
英文:

Vb.Net read and extract data from XML

问题

我一直在尝试多个线程来理解这个问题。我有一个包含x个记录的客户XML文件。我已经从XML中删除了未使用的数据,但需要获取以下Nm,InstAmt,Mmbid和id的值。XML文件将被上传而不保存到磁盘。任何提示都将有帮助。

以下是XML文件的部分内容,其中包含所需的信息:

<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.001.03">
  <CstmrCdtTrfInitn>
    <PmtInf>
      <Dbtr>
        <Nm>CUSTOMER NAME</Nm>
      </Dbtr>
      <CdtTrfTxInf>
        <Amt>
          <InstdAmt Ccy="GBP">220.00</InstdAmt>
        </Amt>
        <CdtrAgt>
          <FinInstnId>
            <ClrSysMmbId>
              <MmbId>010101</MmbId>
            </ClrSysMmbId>
          </FinInstnId>
        </CdtrAgt>
        <CdtrAcct>
          <Id>
            <Othr>
              <Id>02020202</Id>
            </Othr>
          </Id>
        </CdtrAcct>
      </CdtTrfTxInf>
    </PmtInf>
    <PmtInf>
      <Dbtr>
        <Nm>CUSTOMER TWO</Nm>
      </Dbtr>
      <CdtTrfTxInf>
        <Amt>
          <InstdAmt Ccy="GBP">1.00</InstdAmt>
        </Amt>
        <CdtrAgt>
          <FinInstnId>
            <ClrSysMmbId>
              <MmbId>101010</MmbId>
            </ClrSysMmbId>
          </FinInstnId>
        </CdtrAgt>
        <CdtrAcct>
          <Id>
            <Othr>
              <Id>20202020</Id>
            </Othr>
          </Id>
        </CdtrAcct>
      </CdtTrfTxInf>
    </PmtInf>
  </CstmrCdtTrfInitn>
</Document>

希望这可以帮助你获取Nm,InstAmt,Mmbid和id的值。

英文:

I have been trying several threads to try and get my head around this. I have a customer xml file which contains x number of records. I have removed the non used data from the XML but need to get the values for the following Nm , InstAmt, Mmbid, and id. The xml file would uploaded and not saved onto disc. Any pointers would be helpful

&lt;Document xmlns=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;&gt;
  &lt;CstmrCdtTrfInitn&gt;
     &lt;PmtInf&gt;
      &lt;Dbtr&gt;
        &lt;Nm&gt;CUSTOMER NAME&lt;/Nm&gt;
      &lt;/Dbtr&gt;
      &lt;CdtTrfTxInf&gt;
        &lt;Amt&gt;
          &lt;InstdAmt Ccy=&quot;GBP&quot;&gt;220.00&lt;/InstdAmt&gt;
        &lt;/Amt&gt;
        &lt;CdtrAgt&gt;
          &lt;FinInstnId&gt;
            &lt;ClrSysMmbId&gt;
              &lt;MmbId&gt;010101&lt;/MmbId&gt;
            &lt;/ClrSysMmbId&gt;
          &lt;/FinInstnId&gt;
        &lt;/CdtrAgt&gt;
        &lt;CdtrAcct&gt;
          &lt;Id&gt;
            &lt;Othr&gt;
              &lt;Id&gt;02020202&lt;/Id&gt;
            &lt;/Othr&gt;
          &lt;/Id&gt;
        &lt;/CdtrAcct&gt;
      &lt;/CdtTrfTxInf&gt;
    &lt;/PmtInf&gt;
    &lt;PmtInf&gt;
      &lt;Dbtr&gt;
        &lt;Nm&gt;CUSTOMER TWO&lt;/Nm&gt;
      &lt;/Dbtr&gt;
      &lt;CdtTrfTxInf&gt;
        &lt;Amt&gt;
          &lt;InstdAmt Ccy=&quot;GBP&quot;&gt;1.00&lt;/InstdAmt&gt;
        &lt;/Amt&gt;
        &lt;CdtrAgt&gt;
          &lt;FinInstnId&gt;
            &lt;ClrSysMmbId&gt;
              &lt;MmbId&gt;101010&lt;/MmbId&gt;
            &lt;/ClrSysMmbId&gt;
          &lt;/FinInstnId&gt;
        &lt;/CdtrAgt&gt;
        &lt;CdtrAcct&gt;
          &lt;Id&gt;
            &lt;Othr&gt;
              &lt;Id&gt;20202020&lt;/Id&gt;
            &lt;/Othr&gt;
          &lt;/Id&gt;
        &lt;/CdtrAcct&gt;
      &lt;/CdtTrfTxInf&gt;
    &lt;/PmtInf&gt;
  &lt;/CstmrCdtTrfInitn&gt;
&lt;/Document&gt;

答案1

得分: 1

这似乎是ISO 20022格式的Xml。我有一个用于它的Xml模型。我的模型中缺少一些元素,但我已经为您进行了修改。

您可以使用Xml序列化将文件反序列化为.NET对象。

Option Strict On

Imports System.IO
Imports System.Xml.Serialization

' --------- 

Dim s As New XmlSerializer(GetType(Document))
Dim d As Document
Using fs As New FileStream("filename.xml", FileMode.Open)
    d = CType(s.Deserialize(fs), Document)
End Using
For Each p In d.CstmrCdtTrfInitn.PmtInfs
    Debug.Write($"Name: {p.Dbtr.Nm}")
    Debug.Write($", Amt: {p.CdtTrfTxInf.First().Amt.InstdAmt.Value} ({p.CdtTrfTxInf.First().Amt.InstdAmt.Ccy})")
    Debug.Write($", MemberID: {p.CdtTrfTxInf.First().CdtrAgt.FinInstnId.ClrSysMmbId.MmbId}")
    Debug.Write(Environment.NewLine)
Next

在您的情况下的输出:

Name: CUSTOMER NAME, Amt: 220.00 (GBP), MemberID: 010101

Name: CUSTOMER TWO, Amt: 1.00 (GBP), MemberID: 101010

如果不需要,可以注释掉Xml模型中的ISO3166 NuGet包,它用于获取国家代码。

英文:

This looks like Xml format per ISO 20022. I have an Xml model for it. Mine was missing some of your elements but I have modified it for you,

Option Strict On

Imports System.Xml.Serialization

&lt;System.SerializableAttribute(),
System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;),
System.Xml.Serialization.XmlRootAttribute([Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;, IsNullable:=False)&gt;
Partial Public Class Document
    Public Property CstmrCdtTrfInitn As CstmrCdtTrfInitn
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class CstmrCdtTrfInitn
    Public Property GrpHdr As GrpHdr
    &lt;XmlElement(&quot;PmtInf&quot;)&gt;
    Public Property PmtInfs As List(Of PmtInf)
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class GrpHdr
    Public Property MsgId As String
    Public Property CreDtTm As String
        Get
            Return $&quot;{CreDtTmSetValue:yyyy-MM-ddTHH:mm:sszzz}&quot;
        End Get
        Set(value As String)
            Dim parsedValue As DateTime
            If DateTime.TryParse(value, parsedValue) Then CreDtTmSetValue = parsedValue
        End Set
    End Property
    Public Property NbOfTxs As Integer
    Public Property InitgPty As InitgPty
    &lt;XmlIgnore&gt;
    Public Property CreDtTmSetValue As DateTime
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class InitgPty
    Public Property Nm As String
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class PmtInf
    Public Property PmtInfId As String
    Public Property PmtMtd As String
    Public Property PmtTpInf As PmtTpInf
    &lt;System.Xml.Serialization.XmlElementAttribute(DataType:=&quot;date&quot;)&gt;
    Public Property ReqdExctnDt As Date
    Public Property Dbtr As Dbtr
    Public Property DbtrAcct As DbtrAcct
    Public Property DbtrAgt As DbtrAgt
    &lt;System.Xml.Serialization.XmlElementAttribute(&quot;CdtTrfTxInf&quot;)&gt;
    Public Property CdtTrfTxInf As List(Of CdtTrfTxInf)
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class PmtTpInf
    Public Property LclInstrm As LclInstrm
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class LclInstrm
    Public Property Prtry As String
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class Dbtr
    Public Property Nm As String
    Public Property PstlAdr As Adr
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class Adr
    Public Property AdrTp As String
    Public Property Dept As String
    Public Property SubDept As String
    Public Property StrtNm As String
    Public Property BldgNb As String
    Public Property PstCd As String
    Public Property TwnNm As String
    Public Property CtrySubDvsn As String
    Public Property Ctry As String
    &lt;XmlElement(&quot;AdrLine&quot;)&gt;
    Public Property AdrLine As String()
    Public Function ShouldSerializeCtry() As Boolean
        Return CBool(Not AdrLine?.Any())
    End Function
    Public Function ShouldSerializeAdrLine() As Boolean
        Return Not String.IsNullOrEmpty(Ctry)
    End Function
    Shared Function getCountryTwoLetterCode(country As String) As String
        If country.Length = 2 Then Return country
        Dim myCountry = ISO3166.Country.List.Where(Function(c) c.Name.Contains(country)).First()
        Return myCountry.TwoLetterCode
    End Function
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class DbtrAcct
    Public Property Id As DbtrAcctId
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class DbtrAcctId
    Public Property Othr As Othr
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class Othr
    Public Property Id As Integer = 38973836
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class DbtrAgt
    Public Property FinInstnId As FinInstnId
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class FinInstnId
    Public Property BIC As String
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class CdtTrfTxInf
    Public Property PmtId As PmtId
    Public Property Amt As Amt
    Public Property ChqInstr As ChqInstr
    Public Property Cdtr As Cdtr
    Public Property RltdRmtInf As RltdRmtInf
    Public Property RmtInf As RmtInf
    Public Property CdtrAgt As CdtrAgt
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class CdtrAgt
    Public Property FinInstnId As CdtrAgtFinInstnId
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class CdtrAgtFinInstnId
    Public Property ClrSysMmbId As ClrSysMmbId
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class ClrSysMmbId
    Public Property MmbId As String
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class PmtId
    Public Property EndToEndId As String
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class Amt
    Public Property InstdAmt As InstdAmt
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class InstdAmt
    &lt;System.Xml.Serialization.XmlAttributeAttribute()&gt;
    Public Property Ccy As String
    &lt;System.Xml.Serialization.XmlTextAttribute()&gt;
    Public Property Value As String
        Get
            Return $&quot;{SetValue:F2}&quot;
        End Get
        Set(value As String)
            Dim amount As Decimal
            If Decimal.TryParse(value, amount) Then SetValue = amount
        End Set
    End Property
    &lt;XmlIgnore&gt;
    Public Property SetValue As Decimal
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class ChqInstr
    Public Property ChqNb As String
    Public Property DlvrTo As DlvrTo
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class DlvrTo
    Public Property Nm As String
    Public Property Adr As Adr
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class Cdtr
    Public Property Nm As String
    Public Property PstlAdr As Adr
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class RltdRmtInf
    Public Property RmtLctnPstlAdr As RmtLctnPstlAdr
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class RmtLctnPstlAdr
    Public Property Nm As String = &quot;&quot;
    Public Property Adr As Adr
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class RmtInf
    &lt;XmlElement(&quot;Ustrd&quot;)&gt;
    Public Property Ustrds As List(Of Ustrd)
    &lt;XmlElement(&quot;Strd&quot;)&gt;
    Public Property Strds As List(Of Strd)
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class Ustrd
    &lt;XmlText&gt;
    Public Property Text As String
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class Strd
    Public Property RfrdDocInf() As RfrdDocInf
    Public Property RfrdDocAmt As RfrdDocAmt
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class RfrdDocInf
    Public Property Tp As New RmtInfStrdRfrdDocInfTP()
    Public Property Nb As String
    &lt;System.Xml.Serialization.XmlElementAttribute(DataType:=&quot;date&quot;)&gt;
    Public Property RltdDt As Date
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class RfrdDocAmt
    Public Property DuePyblAmt As DuePyblAmt
    Public Property DscntApldAmt As DscntApldAmt
    Public Property CdtNoteAmt As CdtNoteAmt
    Public Property RmtdAmt As RmtdAmt
End Class

&#39;&#39;&#39; &lt;summary&gt;
&#39;&#39;&#39; Credit Amount. Can occur multiple times when paying multiple invoices.
&#39;&#39;&#39; &lt;/summary&gt;
&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Public Class CdtNoteAmt
    &lt;System.Xml.Serialization.XmlAttributeAttribute()&gt;
    Public Property Ccy As String
    &lt;System.Xml.Serialization.XmlTextAttribute()&gt;
    Public Property Value As String
        Get
            Return $&quot;{SetValue:F2}&quot;
        End Get
        Set(value As String)
            Dim amount As Decimal
            If Decimal.TryParse(value, amount) Then SetValue = amount
        End Set
    End Property
    &lt;XmlIgnore&gt;
    Public Property SetValue As Decimal
End Class

&#39;&#39;&#39; &lt;summary&gt;
&#39;&#39;&#39; Invoice Net Amount. Can occur multiple times when paying multiple invoices.
&#39;&#39;&#39; &lt;/summary&gt;
&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Public Class RmtdAmt
    &lt;System.Xml.Serialization.XmlAttributeAttribute()&gt;
    Public Property Ccy As String
    &lt;System.Xml.Serialization.XmlTextAttribute()&gt;
    Public Property Value As String
        Get
            Return $&quot;{SetValue:F2}&quot;
        End Get
        Set(value As String)
            Dim amount As Decimal
            If Decimal.TryParse(value, amount) Then SetValue = amount
        End Set
    End Property
    &lt;XmlIgnore&gt;
    Public Property SetValue As Decimal
End Class

&#39;&#39;&#39; &lt;summary&gt;
&#39;&#39;&#39; Invoice Gross Amount. Can occur multiple times when paying multiple invoices.
&#39;&#39;&#39; &lt;/summary&gt;
&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Public Class DuePyblAmt
    &lt;System.Xml.Serialization.XmlAttributeAttribute()&gt;
    Public Property Ccy As String
    &lt;System.Xml.Serialization.XmlTextAttribute()&gt;
    Public Property Value As String
        Get
            Return $&quot;{SetValue:F2}&quot;
        End Get
        Set(value As String)
            Dim amount As Decimal
            If Decimal.TryParse(value, amount) Then SetValue = amount
        End Set
    End Property
    &lt;XmlIgnore&gt;
    Public Property SetValue As Decimal
End Class

&#39;&#39;&#39; &lt;summary&gt;
&#39;&#39;&#39; Invoice Discount Amount. Can occur multiple times when paying multiple invoices.
&#39;&#39;&#39; &lt;/summary&gt;
&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Public Class DscntApldAmt
    &lt;System.Xml.Serialization.XmlAttributeAttribute()&gt;
    Public Property Ccy As String
    &lt;System.Xml.Serialization.XmlTextAttribute()&gt;
    Public Property Value As String
        Get
            Return $&quot;{SetValue:F2}&quot;
        End Get
        Set(value As String)
            Dim amount As Decimal
            If Decimal.TryParse(value, amount) Then SetValue = amount
        End Set
    End Property
    &lt;XmlIgnore&gt;
    Public Property SetValue As Decimal
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class RmtInfStrdRfrdDocInfTP
    Public Property CdOrPrtry As New CdOrPrtry()
End Class

&lt;System.SerializableAttribute(),
 System.ComponentModel.DesignerCategoryAttribute(&quot;code&quot;),
System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:=&quot;urn:iso:std:iso:20022:tech:xsd:pain.001.001.03&quot;)&gt;
Partial Public Class CdOrPrtry
    Public Property Cd As String
End Class

And you can Xml Serialization to deserialize the file into .net objects

Option Strict On

Imports System.IO
Imports System.Xml.Serialization

&#39; ---------

Dim s As New XmlSerializer(GetType(Document))
Dim d As Document
Using fs As New FileStream(&quot;filename.xml&quot;, FileMode.Open)
    d = CType(s.Deserialize(fs), Document)
End Using
For Each p In d.CstmrCdtTrfInitn.PmtInfs
    Debug.Write($&quot;Name: {p.Dbtr.Nm}&quot;)
    Debug.Write($&quot;, Amt: {p.CdtTrfTxInf.First().Amt.InstdAmt.Value} ({p.CdtTrfTxInf.First().Amt.InstdAmt.Ccy})&quot;)
    Debug.Write($&quot;, MemberID: {p.CdtTrfTxInf.First().CdtrAgt.FinInstnId.ClrSysMmbId.MmbId}&quot;)
    Debug.Write(Environment.NewLine)
Next

Output in your case

>Name: CUSTOMER NAME, Amt: 220.00 (GBP), MemberID: 010101
><br>Name: CUSTOMER TWO, Amt: 1.00 (GBP), MemberID: 101010

Requires NuGet package ISO3166 for country codes in the Xml model, if you don't need it, comment it

答案2

得分: 0

所以,这个工作:

标记:

<asp:GridView ID="GridView1" runat="server"
CssClass="table" Width="60%">
</asp:GridView>

和代码:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    LoadData()
End Sub

Sub LoadData()
    Dim ds As New DataSet
    Dim sFile = Server.MapPath("~/UpLoadFiles/test1.xml")
    ds.ReadXml(sFile)

    Dim rstResult As New DataTable
    For i = 1 To ds.Tables.Count - 1
        Dim dt As DataTable = ds.Tables(i)
        For Each f As DataColumn In dt.Columns
            If rstResult.Columns.Contains(f.ColumnName) = False Then
                Dim fNew As DataColumn = New DataColumn(f.ColumnName, f.DataType)
                rstResult.Columns.Add(fNew)
            End If
        Next
    Next

    For r = 0 To ds.Tables(1).Rows.Count - 1
        Dim NewRow As DataRow = rstResult.NewRow
        For t = 1 To ds.Tables.Count - 1
            For Each f As DataColumn In ds.Tables(t).Columns
                NewRow(f.ColumnName) = ds.Tables(t).Rows(r)(f.ColumnName)
            Next
        Next
        rstResult.Rows.Add(NewRow)
    Next
    GridView1.DataSource = rstResult
    GridView1.DataBind()
End Sub

和结果:

Vb.Net 读取并从 XML 中提取数据

英文:

So, this works:

Markup:

        &lt;asp:GridView ID=&quot;GridView1&quot; runat=&quot;server&quot;
CssClass=&quot;table&quot; Width=&quot;60%&quot;&gt;
&lt;/asp:GridView&gt;

And code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
LoadData()
End Sub
Sub LoadData()
Dim ds As New DataSet
Dim sFile = Server.MapPath(&quot;~/UpLoadFiles/test1.xml&quot;)
ds.ReadXml(sFile)
Dim rstResult As New DataTable
For i = 1 To ds.Tables.Count - 1
Dim dt As DataTable = ds.Tables(i)
For Each f As DataColumn In dt.Columns
If rstResult.Columns.Contains(f.ColumnName) = False Then
Dim fNew As DataColumn = New DataColumn(f.ColumnName, f.DataType)
rstResult.Columns.Add(fNew)
End If
Next
Next
For r = 0 To ds.Tables(1).Rows.Count - 1
Dim NewRow As DataRow = rstResult.NewRow
For t = 1 To ds.Tables.Count - 1
For Each f As DataColumn In ds.Tables(t).Columns
NewRow(f.ColumnName) = ds.Tables(t).Rows(r)(f.ColumnName)
Next
Next
rstResult.Rows.Add(NewRow)
Next
GridView1.DataSource = rstResult
GridView1.DataBind()
End Sub

And result:

Vb.Net 读取并从 XML 中提取数据

huangapple
  • 本文由 发表于 2023年7月11日 00:34:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76655706.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定