Click here to Skip to main content
15,301,841 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to get the Xml Node without looping in asp.net with c#.
Currently I am getting the node value using foreach loop.
But I want to get the node value
<DUEMONTH>202103</DUEMONTH>
without looping

Xml sample is as below:
<ns5:Envelope xmlns:ns5="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns2="http://www.temenos.com/T24/ofs/Response" xmlns:ns3="https://soa.nbf.ae/Enquiry/" xmlns:ns6="http://www.w3.org/2001/XMLSchema" xmlns:ns4="http://www.temenos.com/T24/ofs/ResponseCommon" xmlns:ns1="http://www.temenos.com/T24/ofs/NBFDDSLOANLOOKUPType" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <ns5:Body>
      <ns3:T24EnquiryResponse>
         <Status>SUCCESS</Status>
         <EnquiryOutput>
            <NBFDDSLOANLOOKUP xmlns:ns0="http://www.temenos.com/T24/ofs/Response">
               <gNBFDDSLOANLOOKUPDetailType>
                  <mNBFDDSLOANLOOKUPDetailType>
                     <ID>MG1805700141</ID>
                     <CUSTOMERID>532324</CUSTOMERID>
                     <PRINLIQACCT>012001163238</PRINLIQACCT>
                     <DRAWDOWNACCOUNT>012001163238</DRAWDOWNACCOUNT>
                     <CURRENCY>AED</CURRENCY>
                     <AMOUNT>829500.00</AMOUNT>
                     <LOANSTATUS>LIVE</LOANSTATUS>
                     <OVERDUESTATUS>NAB</OVERDUESTATUS>
                     <DUEMONTH>202103</DUEMONTH>
                     <MONTHDUEAMT>7028.34</MONTHDUEAMT>
                  </mNBFDDSLOANLOOKUPDetailType>
                  <mNBFDDSLOANLOOKUPDetailType>
                     <ID/>
                     <CUSTOMERID/>
                     <PRINLIQACCT/>
                     <DRAWDOWNACCOUNT/>
                     <CURRENCY/>
                     <AMOUNT/>
                     <LOANSTATUS/>
                     <OVERDUESTATUS/>
                     <DUEMONTH>202102</DUEMONTH>
                     <MONTHDUEAMT>7135.61</MONTHDUEAMT>
                  </mNBFDDSLOANLOOKUPDetailType>
                 <pre> </gNBFDDSLOANLOOKUPDetailType>
            </NBFDDSLOANLOOKUP>
         </EnquiryOutput>
      </ns3:T24EnquiryResponse>
   </ns5:Body>
</ns5:Envelope>


What I have tried:

Code is as below:
XmlDocument doc = new XmlDocument();
                       XDocument xdoc = new XDocument();
                       doc.LoadXml(result);
                       var header = doc.SelectNodes("mNBFDDSLOANLOOKUPDetailType");
                       if (header != null)
                       {
                           XmlNodeList nodeList = doc.GetElementsByTagName("mNBFDDSLOANLOOKUPDetailType");

                           foreach (XmlNode node in nodeList)
                           {
                                  OverDueStatus = node["OVERDUESTATUS"].InnerText;
                                   DueMonth = node["DUEMONTH"].InnerText;
                                   MonthlyDueAmount = decimal.Parse(node["MONTHDUEAMT"].InnerText);

if(dueMonth=="201009")
{
//code
}
Posted
Updated 1-Jun-21 4:52am
Comments
PIEBALDconsult 1-Jun-21 1:02am
   
Well, _something_ has to loop.

If you know the exact path to the node then you can use XmlNode.SelectSingleNode Method (System.Xml) | Microsoft Docs[^]

Consider the following example
C#
XmlDocument a = new XmlDocument();
a.LoadXml(@"<ns5:Envelope xmlns:ns5=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:ns2=""http://www.temenos.com/T24/ofs/Response"" xmlns:ns3=""https://soa.nbf.ae/Enquiry/"" xmlns:ns6=""http://www.w3.org/2001/XMLSchema"" xmlns:ns4=""http://www.temenos.com/T24/ofs/ResponseCommon"" xmlns:ns1=""http://www.temenos.com/T24/ofs/NBFDDSLOANLOOKUPType"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
   <ns5:Body>
      <ns3:T24EnquiryResponse>
         <Status>SUCCESS</Status>
         <EnquiryOutput>
            <NBFDDSLOANLOOKUP xmlns:ns0=""http://www.temenos.com/T24/ofs/Response"">
               <gNBFDDSLOANLOOKUPDetailType>
                  <mNBFDDSLOANLOOKUPDetailType>
                     <ID>MG1805700141</ID>
                     <CUSTOMERID>532324</CUSTOMERID>
                     <PRINLIQACCT>012001163238</PRINLIQACCT>
                     <DRAWDOWNACCOUNT>012001163238</DRAWDOWNACCOUNT>
                     <CURRENCY>AED</CURRENCY>
                     <AMOUNT>829500.00</AMOUNT>
                     <LOANSTATUS>LIVE</LOANSTATUS>
                     <OVERDUESTATUS>NAB</OVERDUESTATUS>
                     <DUEMONTH>202103</DUEMONTH>
                     <MONTHDUEAMT>7028.34</MONTHDUEAMT>
                  </mNBFDDSLOANLOOKUPDetailType>
                  <mNBFDDSLOANLOOKUPDetailType>
                     <ID/>
                     <CUSTOMERID/>
                     <PRINLIQACCT/>
                     <DRAWDOWNACCOUNT/>
                     <CURRENCY/>
                     <AMOUNT/>
                     <LOANSTATUS/>
                     <OVERDUESTATUS/>
                     <DUEMONTH>202102</DUEMONTH>
                     <MONTHDUEAMT>7135.61</MONTHDUEAMT>
                  </mNBFDDSLOANLOOKUPDetailType>
               </gNBFDDSLOANLOOKUPDetailType>
            </NBFDDSLOANLOOKUP>
         </EnquiryOutput>
      </ns3:T24EnquiryResponse>
   </ns5:Body>
</ns5:Envelope>");

XmlNamespaceManager ns = new XmlNamespaceManager(a.NameTable);
ns.AddNamespace("ns1", "http://www.temenos.com/T24/ofs/NBFDDSLOANLOOKUPType");
ns.AddNamespace("ns2", "http://www.temenos.com/T24/ofs/Response");
ns.AddNamespace("ns3", "https://soa.nbf.ae/Enquiry/");
ns.AddNamespace("ns4", "http://www.temenos.com/T24/ofs/ResponseCommon");
ns.AddNamespace("ns5", "http://schemas.xmlsoap.org/soap/envelope/");
ns.AddNamespace("ns6", "http://www.w3.org/2001/XMLSchema");

XmlNode b = a.SelectSingleNode("ns5:Envelope/ns5:Body/ns3:T24EnquiryResponse/EnquiryOutput/NBFDDSLOANLOOKUP/gNBFDDSLOANLOOKUPDetailType/mNBFDDSLOANLOOKUPDetailType/DUEMONTH", ns);
   
Comments
ranio 1-Jun-21 1:25am
   
I meant to check DueMonth node value in the Xml (say 202102) if exists I need to take that value alone without looping
Wendelius 1-Jun-21 1:36am
   
You can then use contains. For example
a.SelectSingleNode("//*/DUEMONTH[contains(text(),'202102')]", ns)

Note that depending on the structure and the data of the XML you may have zero, one, or multiple matches.
ranio 1-Jun-21 2:18am
   
that works but how to take the overduestatus node value for that Due Month value
Wendelius 1-Jun-21 6:03am
   
Well you can go back one level and then select the element you like. For example

a.SelectSingleNode("//*/DUEMONTH[contains(text(),'202102')]/../OVERDUESTATUS", ns);

But furthermore, I suggest you get yourself familiar with XPath. For example have a look at XPath Tutorial[^]
ranio 2-Jun-21 6:14am
   
In this I am getting the entire inner text containing above date 2020102
just want the inner text of overduestatus node (NAB) in this Xml
Wendelius 2-Jun-21 6:34am
   
Some typo? If I run for example

a.SelectSingleNode("//*/DUEMONTH[contains(text(),'202103')]/../OVERDUESTATUS", ns)

I get a node and for that node the inner text is

InnerText "NAB" string
This would be trivial to do with LINQ to XML[^]: 😊
C#
const string xml = @"<ns5:Envelope xmlns:ns5=""http://schemas.xmlsoap.org/soap/envelope/"" xmlns:ns2=""http://www.temenos.com/T24/ofs/Response"" xmlns:ns3=""https://soa.nbf.ae/Enquiry/"" xmlns:ns6=""http://www.w3.org/2001/XMLSchema"" xmlns:ns4=""http://www.temenos.com/T24/ofs/ResponseCommon"" xmlns:ns1=""http://www.temenos.com/T24/ofs/NBFDDSLOANLOOKUPType"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"">
   <ns5:Body>
      <ns3:T24EnquiryResponse>
         <Status>SUCCESS</Status>
         <EnquiryOutput>
            <NBFDDSLOANLOOKUP xmlns:ns0=""http://www.temenos.com/T24/ofs/Response"">
               <gNBFDDSLOANLOOKUPDetailType>
                  <mNBFDDSLOANLOOKUPDetailType>
                     <ID>MG1805700141</ID>
                     <CUSTOMERID>532324</CUSTOMERID>
                     <PRINLIQACCT>012001163238</PRINLIQACCT>
                     <DRAWDOWNACCOUNT>012001163238</DRAWDOWNACCOUNT>
                     <CURRENCY>AED</CURRENCY>
                     <AMOUNT>829500.00</AMOUNT>
                     <LOANSTATUS>LIVE</LOANSTATUS>
                     <OVERDUESTATUS>NAB</OVERDUESTATUS>
                     <DUEMONTH>202103</DUEMONTH>
                     <MONTHDUEAMT>7028.34</MONTHDUEAMT>
                  </mNBFDDSLOANLOOKUPDetailType>
                  <mNBFDDSLOANLOOKUPDetailType>
                     <ID/>
                     <CUSTOMERID/>
                     <PRINLIQACCT/>
                     <DRAWDOWNACCOUNT/>
                     <CURRENCY/>
                     <AMOUNT/>
                     <LOANSTATUS/>
                     <OVERDUESTATUS/>
                     <DUEMONTH>202102</DUEMONTH>
                     <MONTHDUEAMT>7135.61</MONTHDUEAMT>
                  </mNBFDDSLOANLOOKUPDetailType>
                 </gNBFDDSLOANLOOKUPDetailType>
            </NBFDDSLOANLOOKUP>
         </EnquiryOutput>
      </ns3:T24EnquiryResponse>
   </ns5:Body>
</ns5:Envelope>";

XDocument document = XDocument.Parse(xml);
string overdueStatus = document.Descendants("mNBFDDSLOANLOOKUPDetailType")
    .Where(n => (string)n.Element("DUEMONTH") == "202103")
    .Select(n => (string)n.Element("OVERDUESTATUS"))
    .FirstOrDefault();
   

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



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