在 C# 中将 Soap XML 转换为 Json 对象

Convert Soap XML to a Json Object in C#

背景信息

我有两个 .net 服务(比如 A 和 B)。服务 B 使用服务 A 的服务引用。此处使用 'basicHttpBinding'。

服务 A 中有一个 global.asax.cs,我计划在将调用发送到服务 A.svc.cs

之前执行一些操作

我可以使用以下代码读取 global.asax.cs 中的请求正文。

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

'message' 字符串变量保存请求主体,即 xml 格式的有效负载。我可以使用以下代码阅读 xml。

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

xml 看起来像这样

<?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>

问题

有什么方法可以将这个 xml 转换成 json 吗?我只对 xml.

里面的数据感兴趣

加分题

'a:sampleProperty'中的'a:'是什么意思/代表什么?

期望输出

最后的json应该是这样的

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

我尝试过的东西

我试过使用代码删除顶级父节点及其属性。然后,我用 JsonConvert 将 xml 转换为 json

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

这样做只对我有部分帮助,我得到以下 json 输出

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

查看已接受的答案 here 以从 XML 中删除名称空间,定义方法 RemoveAllNamespaces 并更改如下代码 -

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 "
                 }
              }
         */

回答你的问题 -

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

标签或属性名称中的 colon (:) 表示元素或属性在 XML namespace 中。colon 和它之前的部分并不是真正的部分标签/属性名称的一部分,它们只是表明它在哪个命名空间中。

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