locked
Extracting a Element using XPathSelectElement RRS feed

  • Question

  • User1015862149 posted

    I am expect on elemt from xml but its returns NUll

    <?xml version="1.0" encoding="utf-8"?>

    <Test1 xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://www.insuranceleads.com/partner/PricePresentationResult.xsd">

    <Test2>OK</Test2>

    <Test3>1439379003</Test3>

    </Test1>

    Am trying to extract test2 element but its return NULL

    var responseXdoc = XDocument.Parse(response);
    var statusElement = responseXdoc.XPathSelectElement("/Test1/Test2");

    result statusElement  as null but am expecting Ok

    Problem in Namespace 

    xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="https://www.insuranceleads.com/partner/PricePresentationResult.xsd (Its my guess)

    please help on this...

    Thanks

    Padmaraj

    Saturday, January 30, 2016 11:52 AM

Answers

  • User-271186128 posted

    Hi Padmaraj,

    I suggest you could try to use XmlDocument.SelectSingleNode() method to get the xml node. Please refer to the following code:

                string xml = Server.MapPath("XMLfiles/XMLFile1.xml");
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(xml);
                
                //add namespace
                XmlNamespaceManager xnm = new XmlNamespaceManager(xmldoc.NameTable);
                xnm.AddNamespace("h", "http://www.w3.org/TR/html4/");
    
                XmlNode node = xmldoc.SelectSingleNode("//Test1/Test2", xnm);
                Response.Write(node.InnerText.ToString());
    
                XmlNode node3 = xmldoc.SelectSingleNode("//Test1/Test3", xnm);
                Response.Write(node3.InnerText.ToString());
    
    
                //XmlNodeList lst = xmldoc.SelectNodes("//Test1/Test2", xnm);
                //StringBuilder sb = new StringBuilder();
                //for (int i = 0; i < lst.Count; i++)
                //{
                //    XmlNode node = lst[i];
                //    XmlElement ele = (XmlElement)node;
                //    sb.AppendLine(ele.InnerText);
                //    sb.AppendLine("<br />");
                //}

    XML file:

    <?xml version="1.0" encoding="utf-8"?>
    <root>
      <Test1 xmlns:h="http://www.w3.org/TR/html4/">
        <Test2>OK</Test2>
        <Test3>1439379003</Test3>
      </Test1>
    </root>

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 1, 2016 5:46 AM

All replies

  • User-271186128 posted

    Hi Padmaraj,

    I suggest you could try to use XmlDocument.SelectSingleNode() method to get the xml node. Please refer to the following code:

                string xml = Server.MapPath("XMLfiles/XMLFile1.xml");
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.Load(xml);
                
                //add namespace
                XmlNamespaceManager xnm = new XmlNamespaceManager(xmldoc.NameTable);
                xnm.AddNamespace("h", "http://www.w3.org/TR/html4/");
    
                XmlNode node = xmldoc.SelectSingleNode("//Test1/Test2", xnm);
                Response.Write(node.InnerText.ToString());
    
                XmlNode node3 = xmldoc.SelectSingleNode("//Test1/Test3", xnm);
                Response.Write(node3.InnerText.ToString());
    
    
                //XmlNodeList lst = xmldoc.SelectNodes("//Test1/Test2", xnm);
                //StringBuilder sb = new StringBuilder();
                //for (int i = 0; i < lst.Count; i++)
                //{
                //    XmlNode node = lst[i];
                //    XmlElement ele = (XmlElement)node;
                //    sb.AppendLine(ele.InnerText);
                //    sb.AppendLine("<br />");
                //}

    XML file:

    <?xml version="1.0" encoding="utf-8"?>
    <root>
      <Test1 xmlns:h="http://www.w3.org/TR/html4/">
        <Test2>OK</Test2>
        <Test3>1439379003</Test3>
      </Test1>
    </root>

    Best regards,
    Dillion

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, February 1, 2016 5:46 AM
  • User1015862149 posted

    Hi Dillion,

      Thanks for your help but why i need to add on namespace, the actual namespace get from some API(Am posting a app to API its returning the response from this response i need to get the Test2 element). 

    Monday, February 1, 2016 6:10 AM
  • User-271186128 posted

    Hi raj1947,<!--?xml:namespace prefix = "o" ns = "urn:schemas-microsoft-com:office:office" /--><o:p></o:p>

    Yes, you are right. Since the Test2 and Test3 doesn't add the namespace prefix, when we query them, there is no need to add the namespace.<o:p></o:p>

    Best regards,
    Dillion<o:p></o:p>

    Tuesday, February 2, 2016 8:27 AM