Convert Soap XML to a Json Object in C#

我的梦境 提交于 2021-02-19 08:39:31

问题


Background Information

I have two .net services (say A and B). Service B uses a service reference of Service A. Here, 'basicHttpBinding' is being used.

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

I'm able to read request body in global.asax.cs using the following code.

StreamReader streamReader = new StreamReader(HttpContext.Current.Request.InputStream);
streamReader.BaseStream.Position = 0;
string message = streamReader.ReadToEnd();

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.

XmlDocument doc = new XmlDocument();
doc.LoadXml(message);

The xml looks like this

<?xml version="1.0" encoding="UTF-8"?>
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
   <s:Body>
      <FunctionName xmlns="http://tempuri.org/">
         <sampleString>value</sampleString>
         <sampleObject xmlns:a="http://schemas.datacontract.org/2004/07/contract" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
            <a:sampleProperty1>value1</a:sampleProperty1>
            <a:sampleProperty2>value2</a:sampleProperty2>
         </sampleObject>
      </FunctionName>
   </s:Body>
</s:Envelope>

Question

Is there any way to convert this xml to json? I'm only interested in the data inside in the xml.

Bonus Question

What does 'a:' in 'a:sampleProperty' mean / stand for?

Desired Output

The final json should like this

{
  "sampleString": "value",
  "sampleObject": {
    "sampleProperty1": "value1",
    "sampleProperty2": "value2"
  }
}

Things that I have tried

I have tried removing top parent nodes and their attributes using code. Then, I used to JsonConvert to convert xml to json

JsonConvert.SerializeXmlNode(doc.ChildNodes[0].ChildNodes[0].ChildNodes[0], Newtonsoft.Json.Formatting.None, true);

Doing this only helped me partially and I ended with the following json output

{
  "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"
  }
}

回答1:


See the accepted answer here to remove the namespaces from the XML, Define a method RemoveAllNamespaces and change the code like below -

XElement xmlDocumentWithoutNs = RemoveAllNamespaces(XElement.Parse(message));
var xmlWithoutNs = xmlDocumentWithoutNs.ToString();
            /* OUTPUT
             <Envelope>
              <Body>
                <FunctionName>
                  <sampleString> value </sampleString>
                  <sampleObject>
                    <sampleProperty1> value1 </sampleProperty1>
                    <sampleProperty2> value2 </sampleProperty2>
                  </sampleObject>
                </FunctionName>
              </Body>
            </Envelope>
             */

 var json  =JsonConvert.SerializeXmlNode(doc.ChildNodes[0].ChildNodes[0].ChildNodes[0], Newtonsoft.Json.Formatting.None, true);
 XmlDocument doc = new XmlDocument();
 doc.LoadXml(xmlWithoutNs);

              /* OUTPUT
              {
               "sampleString":" value ",
               "sampleObject":{
                 "sampleProperty1":" value1 ",
                 "sampleProperty2":" value2 "
                 }
              }
         */

To answer your question -

What does 'a:' in 'a:sampleProperty' mean / stand for?

A colon (:) in a tag or attribute name means that the element or attribute is in an XML namespace.The colon, and the part before it, aren't really part of the tag / attribute name, they just indicate which namespace it's in.

https://en.wikipedia.org/wiki/XML_namespace

Discussions here



来源:https://stackoverflow.com/questions/62299721/convert-soap-xml-to-a-json-object-in-c-sharp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!