Click here to Skip to main content
15,897,187 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have the following XML document that I am trying to read using XDocument. See below code:

XML
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<Document xmlns='urn:iso:std:iso:20022:tech:xsd:pain.002.001.03' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
  <CstmrPmtStsRpt>
    <GrpHdr>
      <MsgId>21169931</MsgId>
      <CreDtTm>2018-11-16T13:40:12</CreDtTm>
      <InitgPty>
        <Nm>Standard Bank SA</Nm>
        <Id>
          <OrgId>
            <BICOrBEI>SBZAZAJJXXX</BICOrBEI>
          </OrgId>
        </Id>
      </InitgPty>
    </GrpHdr>
    <OrgnlGrpInfAndSts>
      <OrgnlMsgId>KESD00017-Domestic Base-300732</OrgnlMsgId>
      <OrgnlMsgNmId>PAIN.001.001.03</OrgnlMsgNmId>
      <OrgnlCreDtTm>2018-11-16T11:11:25</OrgnlCreDtTm>
      <OrgnlNbOfTxs>1</OrgnlNbOfTxs>
      <OrgnlCtrlSum>15000000.00</OrgnlCtrlSum>
      <GrpSts>RJCT</GrpSts>
      <StsRsnInf>
        <Rsn>
          <Cd>NARR</Cd>
        </Rsn>
        <AddtlInf>Error: 4022</AddtlInf>
        <AddtlInf>Duplicate File</AddtlInf>
      </StsRsnInf>
    </OrgnlGrpInfAndSts>
  </CstmrPmtStsRpt>
</Document>


I need to get the information from only 2 tags, namely: <orgnlmsgid> and <addtlinf>

The data should thus be like

string id = "KESD00017-Domestic Base-300732";
string info = "Error: 4022, Duplicate File"; // Concat both separated with a comma.

How can I achieve this?

What I have tried:

I have tried the below just for the id tag but I get the notification: "Empty = "Enumeration yielded no results"

var id = (
from c in doc.Descendants("OrgnlGrpInfAndSts")
select c.Element("OrgnlMsgId").Value
);
Posted
Updated 28-Nov-18 3:08am

1 solution

There is a default namespace.

Everytime you specify an element name, you need to include the namespace:
            XNamespace ns = "urn:iso:std:iso:20022:tech:xsd:pain.002.001.03";
...
            doc.Descendants(ns + "OrgnlGrpInfAndSts");
...
 
Share this answer
 
Comments
Kinyanjui Kamau 29-Nov-18 2:43am    
Cheers, this helps.
Kinyanjui Kamau 29-Nov-18 2:59am    
Thanks a lot for this info.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900