Click here to Skip to main content
15,357,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to check whether Xml node exists in Xml string in asp.net with c#. I tried to load the Xml string and get the node. I want to check if OverDueStatus and DueMonth Node Exists without looping.
My y Xml string is as below:
XML
<T24EnquiryResponse> 
   <Status>SUCCESS</Status>
   <EnquiryOutput>
      <NBFDDSLOANLOOKUP xmlns:ns0="http://www.temenos.com/T24/ofs/Response">
         <gNBFDDSLOANLOOKUPDetailType>
            <mNBFDDSLOANLOOKUPDetailType>
               <ID/>
               <CUSTOMERID/>
               <PRINLIQACCT/>
               <DRAWDOWNACCOUNT/>
               <CURRENCY/>
               <AMOUNT/>
               <LOANSTATUS/>
               <OVERDUESTATUS></OVERDUESTATUS>
               <DUEMONTH>202104</DUEMONTH>
               <MONTHDUEAMT>4248.04</MONTHDUEAMT>
            </mNBFDDSLOANLOOKUPDetailType>
           </gNBFDDSLOANLOOKUPDetailType>
           </NBFDDSLOANLOOKUP>
   </EnquiryOutput>
</T24EnquiryResponse>


What I have tried:

My code is as below:
C#
XmlNode nodeToFind;
                           XmlDocument doc = new XmlDocument();
                           doc.LoadXml(result);

                           // Selects all the title elements that have an attribute named lang

                           XmlElement root = doc.DocumentElement;
                           nodeToFind = root.SelectSingleNode("//OverDueStatus");
                           if (nodeToFind != null)
                           {

                           }
Posted
Updated 22-Sep-21 4:49am
v5
Comments
PIEBALDconsult 22-Sep-21 10:50am
   
Use SelectNodes instead.
Make your XPath expression return both nodes.

1 solution

XmlDocument.Load expects the path of an XML file to load. If your result variable contains the XML as a string, then you need to use LoadXml instead.

XmlDocument.Load Method (System.Xml) | Microsoft Docs[^]
XmlDocument.LoadXml(String) Method (System.Xml) | Microsoft Docs[^]
   
Comments
ranio 22-Sep-21 9:30am
   
That worked.I need to get the node exists in the above xml string without looping.
Tried
XmlNode nodeToFind;
XmlDocument doc = new XmlDocument();
doc.LoadXml(result);

// Selects all the title elements that have an attribute named lang

XmlElement root = doc.DocumentElement;
nodeToFind = root.SelectSingleNode("//OverDueStatus");
if (nodeToFind != null)
{

}

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