使用VB.net读取.xml银行对帐单中特定节点的值

时间:2019-07-17 10:52:43

标签: xml vb.net linq

我正在尝试从xml银行对帐单中提取数据,稍后再将其转换为csv。

我想找到债权人的银行号码,在这里:

<Document xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:camt.053.001.02">
    <BkToCstmrStmt>
        <Stmt>
            <Ntry>
                <NtryDtls>
                    <TxDtls>
                        <RltdPties>
                            <CdtrAcct>
                                <Id>
                                    <IBAN>DE1234567891012345</IBAN>

我正在使用以下代码:

Imports System.Xml
Imports System.Xml.Linq
Imports System.IO

Public Class Form1

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim doc As XDocument = XDocument.Load("bank_statement.xml")
        Dim elements As IEnumerable(Of XElement)
        Dim currentElement As XElement



        elements = doc.Descendants()

        For Each element As XElement In elements

            If element.Name.LocalName.ToString() = "CdtrAcct" Then
                currentElement = element.Descendants()
                If currentElement.Name.LocalName.ToString() = "Id" Then
                    currentElement = currentElement.Descendants()
                    If currentElement.Name.LocalName.ToString() = "IBAN" Then
                        My.Computer.FileSystem.WriteAllText("bank_statement_csv.csv", currentElement.Value.ToString, True)
                    End If
                End If
            End If

        Next

    End Sub
End Class

我有问题,因为它们不是同一类,而且我不知道如何正确声明它

Dim elements As IEnumerable(Of XElement)
Dim currentElement As XElement
element As XElement In elements

您能给我一些诗人,如何解决这个问题?

示例XML银行对帐单:

<Document
    xmlns="urn:iso:std:iso:20022:tech:xsd:camt.053.001.02"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <BkToCstmrStmt>
        <GrpHdr>
            <MsgId>MSGSTMT1234567890123457890</MsgId>
            <CreDtTm>2019-02-13T17:17:18.2369252+01:00</CreDtTm>
        </GrpHdr>
        <Stmt>
            <Id>SKBSTMT5/2019-EUR</Id>
            <LglSeqNb>20195</LglSeqNb>
            <CreDtTm>2019-01-01T00:00:00</CreDtTm>
            <Acct>
                <Id>
                    <IBAN>DE012345678901234</IBAN>
                </Id>
                <Ownr>
                    <Nm>COMPANY NAME</Nm>
                    <PstlAdr>
                        <Ctry>DE</Ctry>
                        <AdrLine>ADDRESS 1</AdrLine>
                        <AdrLine>ADDRESS 2</AdrLine>
                    </PstlAdr>
                </Ownr>
                <Svcr>
                    <FinInstnId>
                        <BIC>BICCODE</BIC>
                        <Nm>BANK NAME</Nm>
                        <PstlAdr>
                            <Ctry>DE</Ctry>
                        </PstlAdr>
                    </FinInstnId>
                </Svcr>
            </Acct>
            <Bal>
                <Tp>
                    <CdOrPrtry>
                        <Cd>OPBD</Cd>
                    </CdOrPrtry>
                </Tp>
                <Amt Ccy="EUR">11000.10</Amt>
                <CdtDbtInd>CRDT</CdtDbtInd>
                <Dt>
                    <Dt>2019-01-01</Dt>
                </Dt>
            </Bal>
            <Bal>
                <Tp>
                    <CdOrPrtry>
                        <Cd>CLBD</Cd>
                    </CdOrPrtry>
                </Tp>
                <Amt Ccy="EUR">1000.10</Amt>
                <CdtDbtInd>CRDT</CdtDbtInd>
                <Dt>
                    <Dt>2019-01-01</Dt>
                </Dt>
            </Bal>
            <TxsSummry>
                <TtlCdtNtries>
                    <NbOfNtries>2</NbOfNtries>
                    <Sum>11000.10</Sum>
                </TtlCdtNtries>
                <TtlDbtNtries>
                    <NbOfNtries>12</NbOfNtries>
                    <Sum>11111.11</Sum>
                </TtlDbtNtries>
            </TxsSummry>
            <Ntry>
                <Amt Ccy="EUR">11.11</Amt>
                <CdtDbtInd>CRDT</CdtDbtInd>
                <Sts>BOOK</Sts>
                <BookgDt>
                    <Dt>2019-01-01</Dt>
                </BookgDt>
                <ValDt>
                    <Dt>2019-01-01</Dt>
                </ValDt>
                <AcctSvcrRef>111111111111</AcctSvcrRef>
                <BkTxCd>
                    <Prtry>
                        <Cd>PP03</Cd>
                    </Prtry>
                </BkTxCd>
                <NtryDtls>
                    <TxDtls>
                        <Refs>
                            <InstrId>12345678901</InstrId>
                            <EndToEndId>NOTPROVIDED</EndToEndId>
                            <TxId>1234567890123456</TxId>
                        </Refs>
                        <RltdPties>
                            <Dbtr>
                                <Nm>DEBITOR NAME</Nm>
                                <PstlAdr>
                                    <Ctry>DE</Ctry>
                                </PstlAdr>
                            </Dbtr>
                            <DbtrAcct>
                                <Id>
                                    <IBAN>DE012345678901234</IBAN>
                                </Id>
                            </DbtrAcct>
                            <Cdtr>
                                <Nm>CREDITOR NAME</Nm>
                                <PstlAdr>
                                    <Ctry>DE</Ctry>
                                </PstlAdr>
                            </Cdtr>
                            <CdtrAcct>
                                <Id>
                                    <IBAN>DE098765432109874</IBAN>
                                </Id>
                            </CdtrAcct>
                        </RltdPties>
                        <RltdAgts>
                            <DbtrAgt>
                                <FinInstnId>
                                    <BIC>BICCODE</BIC>
                                </FinInstnId>
                            </DbtrAgt>
                            <CdtrAgt>
                                <FinInstnId>
                                    <BIC>BICCODE</BIC>
                                </FinInstnId>
                            </CdtrAgt>
                        </RltdAgts>
                        <Purp>
                            <Prtry>OTHR</Prtry>
                        </Purp>
                        <RmtInf>
                            <Ustrd>PAYMENT</Ustrd>
                        </RmtInf>
                    </TxDtls>
                </NtryDtls>
            </Ntry>
            <Ntry>
                <Amt Ccy="EUR">2000.20</Amt>
                <CdtDbtInd>DBIT</CdtDbtInd>
                <Sts>BOOK</Sts>
                <BookgDt>
                    <Dt>2019-01-02</Dt>
                </BookgDt>
                <ValDt>
                    <Dt>2019-01-02</Dt>
                </ValDt>
                <AcctSvcrRef>9876543210</AcctSvcrRef>
                <BkTxCd>
                    <Prtry>
                        <Cd>US03</Cd>
                    </Prtry>
                </BkTxCd>
                <NtryDtls>
                    <TxDtls>
                        <Refs>
                            <InstrId>987654321</InstrId>
                            <EndToEndId>DE987654</EndToEndId>
                            <TxId>898765432109876543210</TxId>
                        </Refs>
                        <RltdPties>
                            <Dbtr>
                                <Nm>DEBITOR NAME</Nm>
                                <PstlAdr>
                                    <Ctry>DE</Ctry>
                                </PstlAdr>
                            </Dbtr>
                            <DbtrAcct>
                                <Id>
                                    <IBAN>DE098765432109874</IBAN>
                                </Id>
                            </DbtrAcct>
                            <Cdtr>
                                <Nm>CREDITOR NAME</Nm>
                                <PstlAdr>
                                    <Ctry>DE</Ctry>
                                    <AdrLine>ADDRESS 3</AdrLine>
                                    <AdrLine>ADDRESS 4</AdrLine>
                                </PstlAdr>
                            </Cdtr>
                            <CdtrAcct>
                                <Id>
                                    <IBAN>DE12345678901234</IBAN>
                                </Id>
                            </CdtrAcct>
                        </RltdPties>
                        <RltdAgts>
                            <DbtrAgt>
                                <FinInstnId>
                                    <BIC>BICCODE</BIC>
                                </FinInstnId>
                            </DbtrAgt>
                            <CdtrAgt>
                                <FinInstnId>
                                    <BIC>BICCODE</BIC>
                                </FinInstnId>
                            </CdtrAgt>
                        </RltdAgts>
                        <Purp>
                            <Prtry>PRCP</Prtry>
                        </Purp>
                        <RmtInf>
                            <Strd>
                                <CdtrRefInf>
                                    <Tp>
                                        <CdOrPrtry>
                                            <Cd>SCOR</Cd>
                                        </CdOrPrtry>
                                    </Tp>
                                    <Ref>123446789</Ref>
                                </CdtrRefInf>
                                <AddtlRmtInf>PAYMENT INFO</AddtlRmtInf>
                            </Strd>
                        </RmtInf>
                    </TxDtls>
                </NtryDtls>
            </Ntry>
        </Stmt>
    </BkToCstmrStmt>
</Document>

1 个答案:

答案 0 :(得分:1)

尝试关注

Imports System.Xml
Imports System.Xml.Linq
Imports System.IO

Module Module1
    Const XML_FILENAME As String = "c:\temp\test.xml"
    Const CSV_FILENAME As String = "c:\temp\test.csv"

    Sub Main()
        Dim csvWriter As New StreamWriter(CSV_FILENAME)
        csvWriter.WriteLine(String.Join(",", {
                                        "Amount",
                                        "Currency",
                                        "Debitor Name",
                                        "Debitor Account",
                                        "Creditor Name",
                                        "Creditor Account"}))

        Dim doc As XDocument = XDocument.Load(XML_FILENAME)
        Dim document As XElement = doc.Root
        Dim ns As XNamespace = document.GetDefaultNamespace()

        For Each Ntry In doc.Descendants(ns + "Ntry")
            Dim amt As XElement = Ntry.Element(ns + "Amt")
            Dim currency As String = CType(amt.Attribute("Ccy"), String)
            Dim amount As Decimal = CType(amt, Decimal)

            Dim Dbtr As XElement = Ntry.Descendants(ns + "Dbtr").FirstOrDefault()
            Dim debitorName As String = CType(Dbtr.Element(ns + "Nm"), String)

            Dim DbtrAcct As XElement = Ntry.Descendants(ns + "DbtrAcct").FirstOrDefault()
            Dim DbtrIBAN As String = CType(DbtrAcct.Descendants(ns + "IBAN").FirstOrDefault(), String)

            Dim Cdtr As XElement = Ntry.Descendants(ns + "Cdtr").FirstOrDefault()
            Dim creditorName As String = CType(Cdtr.Element(ns + "Nm"), String)

            Dim CdtrAcct As XElement = Ntry.Descendants(ns + "CdtrAcct").FirstOrDefault()
            Dim CdtrIBAN As String = CType(CdtrAcct.Descendants(ns + "IBAN").FirstOrDefault(), String)

            csvWriter.WriteLine(String.Join(",", {
                                            amount.ToString(),
                                            currency,
                                            debitorName,
                                            DbtrIBAN,
                                            creditorName,
                                            CdtrIBAN}))


        Next Ntry

        csvWriter.Flush()
        csvWriter.Close()

    End Sub

End Module