XDocument丢失行号

本文关键字:XDocument | 更新日期: 2023-09-27 18:11:20

我使用XDocument来解析我的XML文件,但是当我试图读取XNodeXElement的行号时,它总是等于零。

我尝试了不同的方法来解析它:

foreach (XElement node in xDoc.Root.Descendants("nodeName"))

xDoc.XPathSelectElement("nodeName")

,每次((IXmlLineInfo)node).LineNumber返回0!

我用的是XmlNamespaceManager

我错过什么了吗?

提前感谢!

编辑:

以下是相关的Xml。

<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.008.001.02" xmlns:xsi="http://www.w3.org/2001/XMLSchemainstance">
<CstmrDrctDbtInitn>
<GrpHdr>
  <MsgId>XXXXXXXXX</MsgId>
  <CreDtTm>2013-06-12T00:00:00</CreDtTm>
  <NbOfTxs>1</NbOfTxs>
  <CtrlSum>136.82</CtrlSum>
  <InitgPty>
    <Nm>name</Nm>
    <Id>
      <OrgId>
        <Othr>
          <Id>XXXXXXXXX</Id>
        </Othr>
      </OrgId>
    </Id>
  </InitgPty>
</GrpHdr>
<PmtInf>
  <PmtInfId>275-20130612-FIRST</PmtInfId>
  <PmtMtd>DD</PmtMtd>      
  <BtchBookg>true</BtchBookg>
  <NbOfTxs>1</NbOfTxs>
  <CtrlSum>136.82</CtrlSum>
  <PmtTpInf>
    <SvcLvl>
      <Cd>SEPA</Cd>
    </SvcLvl>
    <LclInstrm>
      <Cd>CORE</Cd>
    </LclInstrm>
    <SeqTp>RCUR</SeqTp>
  </PmtTpInf>
  <ReqdColltnDt>2013-06-05</ReqdColltnDt>
  <Cdtr>
    <Nm>name</Nm>
    <PstlAdr>
      <Ctry>BE</Ctry>
      <AdrLine>XXXXXXXXX</AdrLine>
      <AdrLine>XXXXXXXXX</AdrLine>
    </PstlAdr>
  </Cdtr>
  <CdtrAcct>
    <Id>
      <IBAN>XXXXXXXXX</IBAN>
    </Id>
  </CdtrAcct>
  <CdtrAgt>
    <FinInstnId>
      <BIC>XXXXXXXXX</BIC>
    </FinInstnId>
  </CdtrAgt>
  <ChrgBr>SLEV</ChrgBr>
  <CdtrSchmeId>
    <Id>
      <PrvtId>
        <Othr>
          <Id>XXXXXXXXX</Id>
          <SchmeNm>
            <Prtry>SEPA</Prtry>
          </SchmeNm>
        </Othr>
      </PrvtId>
    </Id>
  </CdtrSchmeId>
  <DrctDbtTxInf>
    <PmtId>
      <InstrId>XXXXXXXXX</InstrId>
      <EndToEndId>XXXXXXXXX</EndToEndId>
    </PmtId>
    <InstdAmt Ccy="EUR">136.82</InstdAmt>
    <DrctDbtTx>
      <MndtRltdInf>
        <MndtId>XXXXXXXXX</MndtId>
        <DtOfSgntr>2009-10-31</DtOfSgntr>
        <AmdmntInd>false</AmdmntInd>
        <AmdmntInfDtls>
          <AmdmntInd>yellowland</AmdmntInd>
          <OrgnlMndtId>XXXXXXXXX</OrgnlMndtId>
          <OrgnlCdtrSchmeId>
            <Id>
              <PrvtId>
              <Othr>
                <Id>XXXXXXXXX</Id>
                <SchmeNm>
                <Prtry>SEPA</Prtry>
                </SchmeNm>
                </Othr>
              </PrvtId>
            </Id>
          </OrgnlCdtrSchmeId>
        </AmdmntInfDtls>
      </MndtRltdInf>
    </DrctDbtTx>
    <DbtrAgt>
        <FinInstnId>
            <BIC>XXXXXXXXX</BIC>
        </FinInstnId>
    </DbtrAgt>
    <Dbtr>
      <Nm>TEST</Nm>
      <PstlAdr>
        <Ctry>BE</Ctry>
        <AdrLine>XXXXXXXXX</AdrLine>
        <AdrLine>XXXXXXXXX</AdrLine>
      </PstlAdr>
    </Dbtr>
    <DbtrAcct>
      <Id>
        <IBAN>XXXXXXXXX</IBAN>
      </Id>
    </DbtrAcct>
    <RmtInf>
      <Ustrd>test</Ustrd>
    </RmtInf>
  </DrctDbtTxInf>
</PmtInf>
</CstmrDrctDbtInitn>
</Document>

XDocument丢失行号

当您通过XDocument加载xml时,lineinformation并不总是被加载。

您需要指定在加载XML时还希望加载LineInformation。这是通过使用Load方法之一来完成的,您可以在XDocument类的LoadOptions中传递值。

var document = XDocument.Load(file, LoadOptions.SetLineInfo);

从这里

XDocument xdoc = XDocument.Load(file);
IEnumerable<XElement> nodes = xdoc.Descendants("nodeName");
foreach (XElement node in nodes)
{
    IXmlLineInfo info = node;
    int lineNumber = info.LineNumber;
}