Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/29.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C语言中将Soap XML转换为Json对象#_C#_Asp.net_Json_Xml_Wcf - Fatal编程技术网

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

C# 在C语言中将Soap XML转换为Json对象#,c#,asp.net,json,xml,wcf,C#,Asp.net,Json,Xml,Wcf,背景信息 我有两个.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.BaseStr

背景信息

我有两个.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();
XmlDocument doc = new XmlDocument();
doc.LoadXml(message);
“message”字符串变量保存请求正文,即xml格式的有效负载。我能够使用以下代码读取xml

StreamReader streamReader = new StreamReader(HttpContext.Current.Request.InputStream);
streamReader.BaseStream.Position = 0;
string message = streamReader.ReadToEnd();
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>
{
  "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"
  }
}
请参阅已接受的答案,以从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 "
                 }
              }
         */
XElement xmlDocumentWithoutNs=RemoveAllNamespaces(XElement.Parse(message));
var xmlWithoutNs=xmlDocumentWithoutNs.ToString();
/*输出
价值
价值1
价值2
*/
var json=JsonConvert.SerializeXmlNode(doc.ChildNodes[0].ChildNodes[0].ChildNodes[0],Newtonsoft.json.Formatting.None,true);
XmlDocument doc=新的XmlDocument();
doc.LoadXml(xmlWithoutNs);
/*输出
{
“sampleString”:“值”,
“sampleObject”:{
“sampleProperty1”:“value1”,
“sampleProperty2”:“value2”
}
}
*/
要回答你的问题——

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

标记或属性名称中的
冒号(:)
表示元素或属性位于
XML命名空间中
冒号及其前面的部分实际上不是标记/属性名称的一部分,它们只是指示它位于哪个命名空间中

请参阅已接受的答案,以从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 "
                 }
              }
         */
XElement xmlDocumentWithoutNs=RemoveAllNamespaces(XElement.Parse(message));
var xmlWithoutNs=xmlDocumentWithoutNs.ToString();
/*输出
价值
价值1
价值2
*/
var json=JsonConvert.SerializeXmlNode(doc.ChildNodes[0].ChildNodes[0].ChildNodes[0],Newtonsoft.json.Formatting.None,true);
XmlDocument doc=新的XmlDocument();
doc.LoadXml(xmlWithoutNs);
/*输出
{
“sampleString”:“值”,
“sampleObject”:{
“sampleProperty1”:“value1”,
“sampleProperty2”:“value2”
}
}
*/
要回答你的问题——

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

标记或属性名称中的
冒号(:)
表示元素或属性位于
XML命名空间中
冒号及其前面的部分实际上不是标记/属性名称的一部分,它们只是指示它位于哪个命名空间中


我也有同样的问题。您有什么理由不能从服务返回JSON吗@SiddheshKulkarni-检查我的回答是否也有助于您的查询!我也有同样的问题,你有什么理由不能从服务中返回JSON吗@SiddheshKulkarni-检查我的回答是否也有助于您的查询!