locked
Unable to get the XML node value RRS feed

  • Question

  • User-1284851152 posted

    Hi Members,

    I am trying to retrieve the value of certain node from XML file, in my asp.net Application.

    Since this is my first ever code using XML, i couldn't successfully retrieve the my desired node value. Could any one please help me on that,

    My XML:

    <?xml version="1.0" encoding="utf-8"?>
    
    <soapenv:Envelope xmlns:csc="https://Somepath" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
    
      <soapenv:Header/>
    
    
      <soapenv:Body>
    
    
       <csc:getMenuEligibleOptionsRequest>
            
                   <test1>abc</test1>    
    
    </csc:getMenuEligibleOptionsRequest>
    
      </soapenv:Body>
    
    </soapenv:Envelope>

    Here i would like to retrieve the value of the node <test1>, for this i have tried the following but unable to solve the error in the first few lines of code.

     var xmldoc = new XmlDocument();
                xmldoc.Load(Server.MapPath(@"\Test.xml"));
    
                XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmldoc.NameTable);
                nsmgr.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
                nsmgr.AddNamespace("csc", "https://Somepath");
                var deviceType = xmldoc.DocumentElement.SelectNodes("/soapenv:Body/", nsmgr);

    I have got the following error from the lastline of code,

    Expression must evaluate to a node-set.

    Monday, October 26, 2015 11:03 AM

Answers

  • User2103319870 posted

    Here i would like to retrieve the value of the node <test1>, for this i have tried the following but unable to solve the error in the first few lines of code.

    I have got the following error from the lastline of code,

    Expression must evaluate to a node-set.

    You need to map the node accordingly and ensure that you provided till the parent node to get value properly.

    You can try with the below code

     var xmldoc = new XmlDocument();
                    xmldoc.Load(Server.MapPath(@"\Test.xml"));
    
                    //Create NameSpaceManager
                    XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmldoc.NameTable);
                    //Add the namespace of xml here
                    nsmgr.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
                    nsmgr.AddNamespace("csc", "https://Somepath");
                    //Get the test1 number node
                    XmlNode test1node = xmldoc.SelectSingleNode("soapenv:Envelope/soapenv:Body/csc:getMenuEligibleOptionsRequest/test1", nsmgr);
                    //Get the text1
                    string test1vale = test1node.InnerText;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 26, 2015 11:26 AM

All replies

  • User2103319870 posted

    Here i would like to retrieve the value of the node <test1>, for this i have tried the following but unable to solve the error in the first few lines of code.

    I have got the following error from the lastline of code,

    Expression must evaluate to a node-set.

    You need to map the node accordingly and ensure that you provided till the parent node to get value properly.

    You can try with the below code

     var xmldoc = new XmlDocument();
                    xmldoc.Load(Server.MapPath(@"\Test.xml"));
    
                    //Create NameSpaceManager
                    XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmldoc.NameTable);
                    //Add the namespace of xml here
                    nsmgr.AddNamespace("soapenv", "http://schemas.xmlsoap.org/soap/envelope/");
                    nsmgr.AddNamespace("csc", "https://Somepath");
                    //Get the test1 number node
                    XmlNode test1node = xmldoc.SelectSingleNode("soapenv:Envelope/soapenv:Body/csc:getMenuEligibleOptionsRequest/test1", nsmgr);
                    //Get the text1
                    string test1vale = test1node.InnerText;

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, October 26, 2015 11:26 AM
  • User-1284851152 posted

    Appreciate your immediate reply. Thanks a lot

    Monday, October 26, 2015 1:00 PM