1

I have a soap response, I want to remove all soap related things and get the inner XML with only one header.

response:

"<s:Envelope xmlns:xsd=""http://www.w3.org/2001/XMLSchema"" xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" xmlns:s=""http://schemas.xmlsoap.org/soap/envelope/"">
    <s:Body>
        <getResponse xmlns = ""http://tempuri.org/"">
            <getResult xmlns:xsi = ""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd = ""http://www.w3.org/2001/XMLSchema"">
                <Id>999</Id>
                <EResponse>
                    <au>
                        <st>ABC</st>
                        <co>DGH</co>
                        <mo>MMM</mo>
                    </au>
         </EResponse>
            </getResult>
        </getResponse>
    </s:Body>
</s:Envelope> 
          

I want to extract this:

<getResult xmlns:xsi = ""http://www.w3.org/2001/XMLSchema-instance"" xmlns:xsd = ""http://www.w3.org/2001/XMLSchema"">
                <Id>999</Id>
                <EResponse>
                    <au>
                        <st>ABC</st>
                        <co>DGH</co>
                        <mo>MMM</mo>
                    </au>
         </EResponse>
            </getResult>

I am trying this:

XDocument input;

      using (var nodeReader = new XmlNodeReader(doc))
        {
            nodeReader.MoveToContent();
            input = XDocument.Load(nodeReader);
        }

        var xns = XNamespace.Get("http://tempuri.org/");
        var SoapBody = input.Descendants(xns + "getResult").First().ToString();

but I am also getting namespace tempuri appended in getresult tag, which I don't want or any other way to achieve it ?

cSharp
  • 25
  • 1
  • 8
  • the reason you are getting the namespace in your result is because in the upper element the namespace is defined as xmlns="http://tempuri.org/" which means all elements and attributes that are a child of this element will have this particular namespace. If you really need the content without the namespaces you need to add an additional step to remove all the namespaces from your result. – martijn Sep 23 '20 at 09:26
  • m also thinking the same, only way is to remove the namespaces, actually my ultimate aim is to de-serialize it into my class which is named differently ,say GetResultTrigger. any insight ? – cSharp Sep 23 '20 at 11:28
  • to remove the namespaces in your xml document you can follow the marked answer from here: https://stackoverflow.com/questions/987135/how-to-remove-all-namespaces-from-xml-with-c Using a class does not help you in removing the namespaces. – martijn Sep 23 '20 at 12:02

1 Answers1

1

Try following :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.IO;

namespace ConsoleApplication169
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            string xml = File.ReadAllText(FILENAME);
            XDocument doc = XDocument.Parse(xml);


            XElement results = doc.Descendants().Where(x=> x.Name.LocalName == "getResult").FirstOrDefault();
            XNamespace ns = results.GetDefaultNamespace();

            string id = (string)results.Element(ns + "Id");

            Dictionary<string, string> au = results.Descendants(ns + "au").FirstOrDefault().Elements()
                .GroupBy(x => x.Name.LocalName, y => (string)y)
                .ToDictionary(x => x.Key, y => y.FirstOrDefault());

        }
    }
}
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • 1
    thanks for your reply, however i am trying to get an xml starting from data. above solution will discard and the root tag, plus what if there is more data (payload) within root tag – cSharp Sep 23 '20 at 11:25
  • The data always comes in the Response. My code extras all the values in the xml. – jdweng Sep 23 '20 at 12:18