Answered XPATH : Extract data from Soap Message

  • Saturday, May 19, 2012 8:58 AM
     
     

    Hi,

    I have got the following soap message and would like to know how to write xpath query to extract the GetNewGUIDResult?

    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
      <s:Header>
        <ActivityId CorrelationId="06996737-224f-4004-9dad-042222b161fc" xmlns="http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics">b89cdee8-18b4-4ea0-a5a1-7b10ace1b23e</ActivityId>
      </s:Header>
      <s:Body>
        <GetNewGUIDResponse xmlns="http://bank.co.com/Service/2011_01/Service">
          <GetNewGUIDResult>125959</GetNewGUIDResult>
        </GetNewGUIDResponse>
      </s:Body>
    </s:Envelope>

    I tried some some xpath commands but they are giving exceptions. Also I am not sure about the usage of xmlnamespacemanager.

    Thanks

    Amare



    • Edited by Amare1982 Saturday, May 19, 2012 8:59 AM
    •  

All Replies

  • Saturday, May 19, 2012 9:31 AM
     
     Answered Has Code

    Here is a sample:

                XmlDocument doc = new XmlDocument();
                doc.Load("soap.xml");
                XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
                mgr.AddNamespace("bc", "http://bank.co.com/Service/2011_01/Service");
                XmlNode result = doc.SelectSingleNode("//bc:GetNewGUIDResult", mgr);
                if (result != null)
                {
                    Console.WriteLine("Found {0}.", result.InnerText);
                }
                else
                {
                    // handle case that no element was found
                    Console.WriteLine("No element found.");
                }
    So basically you need to choose a prefix (as I did it with "bc") and then use AddNamespace to bind it to the namespace the element you are looking for is in, then use that prefix to qualify the element name in the path.


    MVP Data Platform Development My blog

    • Marked As Answer by Amare1982 Sunday, May 20, 2012 3:43 AM
    •  
  • Saturday, May 19, 2012 1:06 PM
     
     Answered Has Code
    using System;
    using System.Xml;
    using System.Xml.Linq;
    
    namespace _3b3f3cae_713e_48e6_8576_d1e96a4fac79
    {
        class Program
        {
            static void Main(string[] args)
            {
                const string xml =
                    @"<s:Envelope xmlns:s='http://schemas.xmlsoap.org/soap/envelope/'>
                          <s:Header>
                            <ActivityId CorrelationId='06996737-224f-4004-9dad-042222b161fc' 
                              xmlns='http://schemas.microsoft.com/2004/09/ServiceModel/Diagnostics'>
                              b89cdee8-18b4-4ea0-a5a1-7b10ace1b23e
                            </ActivityId>
                          </s:Header>
                          <s:Body>
                            <GetNewGUIDResponse xmlns='http://bank.co.com/Service/2011_01/Service'>
                              <GetNewGUIDResult>125959</GetNewGUIDResult>
                            </GetNewGUIDResponse>
                          </s:Body>
                      </s:Envelope>";
    
                XmlDocument xmldoc = new XmlDocument();
                xmldoc.LoadXml(xml);
                XmlNamespaceManager xmanager = new XmlNamespaceManager(xmldoc.NameTable);
                xmanager.AddNamespace("s", "http://schemas.xmlsoap.org/soap/envelope/");
                xmanager.AddNamespace("x", "http://bank.co.com/Service/2011_01/Service");
    
                XmlNode getNewGUIDResultNode = xmldoc.SelectSingleNode("s:Envelope/s:Body/x:GetNewGUIDResponse/x:GetNewGUIDResult", xmanager);
                string getNewGUIDResultValue = getNewGUIDResultNode.InnerText;
    
                Console.WriteLine("\ns:Envelope/s:Body/x:GetNewGUIDResponse/x:GetNewGUIDResult");
                Console.WriteLine(getNewGUIDResultValue);
    
                // Bonus:
    
                Console.WriteLine("\nWithout XPATH:");
    
                XDocument xdoc = XDocument.Parse(xml);
                XNamespace s = "http://schemas.xmlsoap.org/soap/envelope/";
                XNamespace x = "http://bank.co.com/Service/2011_01/Service";
                var val = xdoc.Element(s + "Envelope")
                              .Element(s + "Body")
                              .Element(x + "GetNewGUIDResponse")
                              .Element(x + "GetNewGUIDResult")
                              .Value;
    
                Console.WriteLine(val);
    
                Console.ReadKey();
            }
        }
    }
    


    My blog

    Whether you’re a construction worker, a forum moderator, or just someone that likes helping people. I think these guidelines can be helpful in keeping you helpful when being helpful.

    • Marked As Answer by Amare1982 Sunday, May 20, 2012 3:43 AM
    •  
  • Sunday, May 20, 2012 3:44 AM
     
     
    Thanks Martin and Link.fr ! That solved my problem. Thank you.