使用 XPathEvaluate 从 camt053 xml 文件读取数据

Reading Data from camt053 xml file with XPathEvaluate

我尝试从 camt053 xml 文件中读取一些特定值,例如帐户的 IBAN 我阅读了一些教程并尝试了这样的方法,但不幸的是它不起作用。 我尝试了一些差异等,但我不知道我的错误在哪里? 像这样和我的教程很相似

static void Main(string[] args)
        {
            
            XDocument xdoc = XDocument.Load(@"C:\Users\M\Desktop\test.xml");
            var value = "//Acct/Id";
            var result = xdoc.XPathEvaluate(value);
            Console.WriteLine(result);
            Console.ReadKey();
        }

它总是给我这个错误: System.Xml.XPath.XPathEvaluator+d__1`1[System.Object]

我的文件是 camt053 示例:

<?xml version="1.0" encoding="UTF-8"?>

-<Document xsi:schemaLocation="urn:iso:std:iso:20022:tech:xsd:camt.053.001.02 camt.053.001.02.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:camt.053.001.02">


-<BkToCstmrStmt>


-<GrpHdr>

<MsgId>053D2013-12-27T22:05:03.0N130000005</MsgId>

<CreDtTm>2013-12-27T22:04:52.0+01:00</CreDtTm>


-<MsgPgntn>

<PgNb>1</PgNb>

<LastPgInd>true</LastPgInd>

</MsgPgntn>

</GrpHdr>


-<Stmt>

<Id>0352C5320131227220503</Id>

<ElctrncSeqNb>130000005</ElctrncSeqNb>

<CreDtTm>2013-12-27T22:04:52.0+01:00</CreDtTm>


-<Acct>


-<Id>

<IBAN>DE14740618130000033626</IBAN>

</Id>

<Ccy>EUR</Ccy>


-<Ownr>

<Nm>Testkonto Nummer 1</Nm>

</Ownr>


-<Svcr>


-<FinInstnId>

<BIC>GENODEF1PFK</BIC>

<Nm>VR-Bank Rottal-Inn eG</Nm>


-<Othr>

<Id>DE 129267947</Id>

<Issr>UmsStId</Issr>

</Othr>

</FinInstnId>

</Svcr>

</Acct>


-<Bal>


-<Tp>


-<CdOrPrtry>

<Cd>PRCD</Cd>

</CdOrPrtry>

</Tp>

<Amt Ccy="EUR">33.06</Amt>

<CdtDbtInd>CRDT</CdtDbtInd>


-<Dt>

<Dt>2013-12-27</Dt>

</Dt>

</Bal>


-<Bal>


-<Tp>


-<CdOrPrtry>

<Cd>CLBD</Cd>

</CdOrPrtry>

</Tp>

<Amt Ccy="EUR">23.06</Amt>

<CdtDbtInd>CRDT</CdtDbtInd>


-<Dt>

<Dt>2013-12-27</Dt>

</Dt>

</Bal>


-<Ntry>

<Amt Ccy="EUR">2.00</Amt>

<CdtDbtInd>DBIT</CdtDbtInd>

<Sts>BOOK</Sts>


-<BookgDt>

<Dt>2013-12-27</Dt>

</BookgDt>


-<ValDt>

<Dt>2013-12-27</Dt>

</ValDt>

<AcctSvcrRef>2013122710583450000</AcctSvcrRef>

<BkTxCd/>


-<NtryDtls>


-<TxDtls>


-<BkTxCd>


-<Prtry>

<Cd>NTRF+020</Cd>

<Issr>ZKA</Issr>

</Prtry>

</BkTxCd>


-<RltdPties>


-<Cdtr>

<Nm>Testkonto Nummer 2</Nm>

</Cdtr>


-<CdtrAcct>


-<Id>


-<Othr>

<Id> 740618130100033626</Id>


-<SchmeNm>

<Cd>BBAN</Cd>

</SchmeNm>

</Othr>

</Id>

</CdtrAcct>

</RltdPties>


-<RmtInf>

<Ustrd>TEST BERWEISUNG MITTELS BLZUND KONTONUMMER - DTA</Ustrd>

</RmtInf>

</TxDtls>

</NtryDtls>

</Ntry>


-<Ntry>

<Amt Ccy="EUR">3.00</Amt>

<CdtDbtInd>DBIT</CdtDbtInd>

<Sts>BOOK</Sts>


-<BookgDt>

<Dt>2013-12-27</Dt>

</BookgDt>


-<ValDt>

<Dt>2013-12-27</Dt>

</ValDt>

<AcctSvcrRef>2013122710583600000</AcctSvcrRef>

<BkTxCd/>


-<NtryDtls>


-<TxDtls>


-<Refs>

<MsgId>CCTI/VRNWSW/b044f24cddb92a502b8a1b5</MsgId>

<EndToEndId>NOTPROVIDED</EndToEndId>

</Refs>


-<BkTxCd>


-<Prtry>

<Cd>NMSC+201</Cd>

<Issr>ZKA</Issr>

</Prtry>

</BkTxCd>


-<RltdPties>


-<Dbtr>

<Nm>Testkonto Nummer 1</Nm>

</Dbtr>


-<DbtrAcct>


-<Id>

<IBAN>DE14740618130000033626</IBAN>

</Id>

</DbtrAcct>


-<UltmtDbtr>

<Nm>keine Information vorhanden</Nm>

</UltmtDbtr>


-<Cdtr>

<Nm>Testkonto Nummer 2</Nm>

</Cdtr>


-<CdtrAcct>


-<Id>

<IBAN>DE58740618130100033626</IBAN>

</Id>

</CdtrAcct>


-<UltmtCdtr>

<Nm>keine Information vorhanden</Nm>

</UltmtCdtr>

</RltdPties>


-<RltdAgts>


-<CdtrAgt>


-<FinInstnId>

<BIC>GENODEF1PFK</BIC>

</FinInstnId>

</CdtrAgt>

</RltdAgts>


-<RmtInf>

<Ustrd>Test+berweisung mit BIC und IBAN SEPA IBAN: DE58740618130100033626 BIC: GENODEF1PFK</Ustrd>

</RmtInf>

</TxDtls>

</NtryDtls>

</Ntry>


-<Ntry>

<Amt Ccy="EUR">1.00</Amt>

<CdtDbtInd>CRDT</CdtDbtInd>

<Sts>BOOK</Sts>


-<BookgDt>

<Dt>2013-12-27</Dt>

</BookgDt>


-<ValDt>

<Dt>2013-12-27</Dt>

</ValDt>

<AcctSvcrRef>2013122711085260000</AcctSvcrRef>

<BkTxCd/>


-<NtryDtls>


-<TxDtls>


-<BkTxCd>


-<Prtry>

<Cd>NMSC+051</Cd>

<Issr>ZKA</Issr>

</Prtry>

</BkTxCd>


-<RltdPties>


-<Dbtr>

<Nm>Testkonto Nummer 2</Nm>

</Dbtr>


-<DbtrAcct>


-<Id>


-<Othr>

<Id> 740618130100033626</Id>


-<SchmeNm>

<Cd>BBAN</Cd>

</SchmeNm>

</Othr>

</Id>

</DbtrAcct>

</RltdPties>


-<RmtInf>

<Ustrd>R CKBUCHUNG</Ustrd>

</RmtInf>

</TxDtls>

</NtryDtls>

</Ntry>


-<Ntry>

<Amt Ccy="EUR">6.00</Amt>

<CdtDbtInd>DBIT</CdtDbtInd>

<Sts>BOOK</Sts>


-<BookgDt>

<Dt>2013-12-27</Dt>

</BookgDt>


-<ValDt>

<Dt>2013-12-27</Dt>

</ValDt>

<AcctSvcrRef>2013122711513230000</AcctSvcrRef>

<BkTxCd/>


-<NtryDtls>


-<Btch>

<PmtInfId>STZV-PmInf27122013-11:02-2</PmtInfId>

<NbOfTxs>2</NbOfTxs>

</Btch>


-<TxDtls>


-<Refs>

<MsgId>STZV-Msg27122013-11:02</MsgId>

<EndToEndId>STZV-EtE27122013-11:02-1</EndToEndId>

</Refs>


-<AmtDtls>


-<TxAmt>

<Amt Ccy="EUR">3.50</Amt>

</TxAmt>

</AmtDtls>


-<BkTxCd>


-<Prtry>

<Cd>NMSC+201</Cd>

<Issr>ZKA</Issr>

</Prtry>

</BkTxCd>


-<RltdPties>


-<Dbtr>

<Nm>Testkonto Nummer 2</Nm>

</Dbtr>


-<DbtrAcct>


-<Id>

<IBAN>DE58740618130100033626</IBAN>

</Id>

</DbtrAcct>


-<UltmtDbtr>

<Nm>keine Information vorhanden</Nm>

</UltmtDbtr>


-<Cdtr>

<Nm>Testkonto Nummer 1</Nm>

</Cdtr>


-<CdtrAcct>


-<Id>

<IBAN>DE14740618130000033626</IBAN>

</Id>

</CdtrAcct>


-<UltmtCdtr>

<Nm>Testkonto</Nm>

</UltmtCdtr>

</RltdPties>


-<RmtInf>

<Ustrd>Sammelueberwseisung 2. Zahlung TAN:283044 </Ustrd>

</RmtInf>

</TxDtls>


-<TxDtls>


-<Refs>

<MsgId>STZV-Msg27122013-11:02</MsgId>

<EndToEndId>STZV-EtE27122013-11:02-2</EndToEndId>

</Refs>


-<AmtDtls>


-<TxAmt>

<Amt Ccy="EUR">2.50</Amt>

</TxAmt>

</AmtDtls>


-<BkTxCd>


-<Prtry>

<Cd>NMSC+201</Cd>

<Issr>ZKA</Issr>

</Prtry>

</BkTxCd>


-<RltdPties>


-<Dbtr>

<Nm>Testkonto Nummer 2</Nm>

</Dbtr>


-<DbtrAcct>


-<Id>

<IBAN>DE58740618130100033626</IBAN>

</Id>

</DbtrAcct>


-<UltmtDbtr>

<Nm>keine Information vorhanden</Nm>

</UltmtDbtr>


-<Cdtr>

<Nm>Testkonto Nummer 1</Nm>

</Cdtr>


-<CdtrAcct>


-<Id>

<IBAN>DE14740618130000033626</IBAN>

</Id>

</CdtrAcct>


-<UltmtCdtr>

<Nm>Testkonto</Nm>

</UltmtCdtr>

</RltdPties>


-<RmtInf>

<Ustrd>Sammelueberweisung 1. Zahlung TAN:283044 </Ustrd>

</RmtInf>

</TxDtls>

</NtryDtls>

</Ntry>

</Stmt>

</BkToCstmrStmt>

</Document>

尝试以下操作:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement root = doc.Root;
            XNamespace ns = root.GetDefaultNamespace();

            XElement acct = root.Descendants(ns + "Acct").FirstOrDefault();
            string id = (string)acct.Descendants(ns + "Id").FirstOrDefault();
        }
    }
}

我正在处理完全相同的问题。我能够通过像这样删除名称空间信息来解决它:

static void Main(string[] args)
{
    using var fs = new FileStream("<path to filename>", FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
    var document = XDocument.Load(fs);

    NormalizeElementNamespaces(document);

    var resultEnum = (IEnumerable)document.XPathEvaluate("//Acct/Id");
    var elemEnum = resultEnum.Cast<XElement>();
    foreach (var elem in elemEnum)
    {
        var acctId = elem.Value;
    }
}

static void NormalizeElementNamespaces(XDocument document)
{
    if (document?.Root == null) return;

    // Remove all xmlns, xmlns:xsi, xsi:schemaLocation, etc.
    while (document.Root.HasAttributes)
    {
        document.Root.FirstAttribute.Remove();
    }
    
    // Remove namespaces from attributes and elements
    document.Descendants().Attributes().Where(x => x.IsNamespaceDeclaration).Remove();
    foreach (var element in document.Descendants())
        element.Name = element.Name.LocalName;
}