且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-01-23 08:31:51

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

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

回答你的问题 -

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

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

标签或属性名称中的 冒号 (:) 表示该元素或属性位于 XML 命名空间中.冒号,以及它之前的部分,并不是真正的标签/属性名称的一部分,它们只是表明它在哪个命名空间中.

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

此处讨论