XDocument losing line number

c#, linq-to-xml, xml

Solution

The LineInformations are not always loaded, when you load xml via `XDocument`.

You need to specify that you also want to load the LineInformation when you load the XML. That is done by using one of the Load methods that you can pass in a value of `LoadOptions` of the `XDocument` class.

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

Problem

I'm using `XDocument` to parse my XML File, but when I try to read the line number of a `XNode` or a `XElement`, it's always equal to zero. I tried different ways to parse it: ``` foreach (XElement node in xDoc.Root.Descendants("nodeName")) ``` or ``` xDoc.XPathSelectElement("nodeName") ``` and each time `((IXmlLineInfo)node).LineNumber` returns 0! I'm using a `XmlNamespaceManager`. Did I miss something? Thanks in advance! Edit : Here is the concerned Xml as asked. ``` <?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> ```

Original source

Related problems