title: Convert Soap XML to a Json Object in C#
body: <p><strong>Background Information</strong></p>
  
  <p>I have two .net services (say A and B). Service B uses a service reference of Service A. Here, 'basicHttpBinding' is being used.</p>
  
  <p>There is a global.asax.cs present in Service A where I plan to perform some operations before the call is sent to Service A.svc.cs</p>
  
  <p>I'm able to read request body in global.asax.cs using the following code.</p>
  
  <pre><code>StreamReader streamReader = new StreamReader(HttpContext.Current.Request.InputStream);
  streamReader.BaseStream.Position = 0;
  string message = streamReader.ReadToEnd();
  </code></pre>
  
  <p>The 'message' string variable holds the request body i.e. payload in xml format. I'm able to read the xml using the following code.</p>
  
  <pre><code>XmlDocument doc = new XmlDocument();
  doc.LoadXml(message);
  </code></pre>
  
  <p>The xml looks like this</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"&gt;
     &lt;s:Body&gt;
        &lt;FunctionName xmlns="http://tempuri.org/"&gt;
           &lt;sampleString&gt;value&lt;/sampleString&gt;
           &lt;sampleObject xmlns:a="http://schemas.datacontract.org/2004/07/contract" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"&gt;
              &lt;a:sampleProperty1&gt;value1&lt;/a:sampleProperty1&gt;
              &lt;a:sampleProperty2&gt;value2&lt;/a:sampleProperty2&gt;
           &lt;/sampleObject&gt;
        &lt;/FunctionName&gt;
     &lt;/s:Body&gt;
  &lt;/s:Envelope&gt;
  </code></pre>
  
  <p><strong>Question</strong></p>
  
  <p>Is there any way to convert this xml to json? I'm only interested in the data inside in the xml.</p>
  
  <p><strong>Bonus Question</strong></p>
  
  <p>What does 'a:' in 'a:sampleProperty' mean / stand for?</p>
  
  <p><strong>Desired Output</strong></p>
  
  <p>The final json should like this</p>
  
  <pre><code>{
    "sampleString": "value",
    "sampleObject": {
      "sampleProperty1": "value1",
      "sampleProperty2": "value2"
    }
  }
  </code></pre>
  
  <p><strong>Things that I have tried</strong></p>
  
  <p>I have tried removing top parent nodes and their attributes using code. Then, I used to JsonConvert to convert xml to json</p>
  
  <pre><code>JsonConvert.SerializeXmlNode(doc.ChildNodes[0].ChildNodes[0].ChildNodes[0], Newtonsoft.Json.Formatting.None, true);
  </code></pre>
  
  <p>Doing this only helped me partially and I ended with the following json output</p>
  
  <pre><code>{
    "sampleString": "value",
    "sampleObject": {
      "@xmlns:a":"http://schemas.datacontract.org/2004/07/contract",
      "@xmlns:i":"http://www.w3.org/2001/XMLSchema-instance",
      "a:sampleProperty1": "value1",
      "a:sampleProperty2": "value2"
    }
  }
  </code></pre>