【问题标题】:Convert Soap XML to a Json Object in C#在 C# 中将 Soap XML 转换为 Json 对象
【发布时间】:2020-09-29 15:23:05
【问题描述】:

背景信息

我有两个 .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();

“消息”字符串变量保存请求正文,即 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"
  }
}

【问题讨论】:

标签: c# asp.net json xml wcf


【解决方案1】:

查看接受的答案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 "
                 }
              }
         */

回答你的问题 -

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

标签或属性名称中的colon (:) 表示元素或属性位于XML namespace 中。colon 及其前面的部分并不是标签/属性名称的真正组成部分,它们只需指出它在哪个命名空间中。

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

Discussions here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多