在 C# 中将 Soap XML 转换为 Json 对象
背景信息
我有两个 .net 服务(比如 A 和 B).服务 B 使用服务 A 的服务引用.这里使用的是basicHttpBinding".
I have two .net services (say A and B). Service B uses a service reference of Service A. Here, 'basicHttpBinding' is being used.
服务 A 中存在 global.asax.cs,我计划在将调用发送到服务 A.svc.cs 之前执行一些操作
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
我可以使用以下代码读取 global.asax.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();
'message' 字符串变量保存请求正文,即 xml 格式的有效负载.我可以使用以下代码读取 xml.
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);
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里面的数据感兴趣.
Is there any way to convert this xml to json? I'm only interested in the data inside in the xml.
额外问题
a:sampleProperty"中的a:"是什么意思/代表什么?
What does 'a:' in 'a:sampleProperty' mean / stand for?
期望输出
最终的json应该是这样的
The final json should like this
{
"sampleString": "value",
"sampleObject": {
"sampleProperty1": "value1",
"sampleProperty2": "value2"
}
}
我尝试过的事情
我尝试使用代码删除顶级父节点及其属性.然后,我用 JsonConvert 将 xml 转换为 json
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);
这样做只能部分帮助我,我以以下 json 输出结束
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"
}
}
查看已接受的答案 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.