【发布时间】:2018-12-04 03:53:20
【问题描述】:
我有一个字符串 xml
?xml version="1.0" encoding="UTF-8"?>
<Entities TotalResults="64">
<Entity Type="test-set">
<ChildrenCount>
<Value>0</Value>
</ChildrenCount>
<Fields>
<Field Name="name">
<Value>default</Value>
</Field>
<Field Name="id">
<Value>0</Value>
</Field>
</Fields>
<RelatedEntities />
</Entity>
<Entity Type="test-set">
<ChildrenCount>
<Value>0</Value>
</ChildrenCount>
<RelatedEntities />
</Entity>
<singleElementCollection>false</singleElementCollection>
</Entities>
我正在使用下面的代码将 xml 字符串转换为 json 并使用 XMLDocument 和 JsonConvert.serializexmlNode()。
XmlDocument doc = new XmlDocument();
string xml = objHttpWebClient._retunResponseStream("http://test:8080/test-sets?fields=ID,Name", "GET", ASCIIEncoding.UTF8, cc);
doc.LoadXml(xml);
string jsonText = JsonConvert.SerializeXmlNode(doc);
return jsonText;
我得到如下输出
{
"?xml": {
"@version": "1.0",
"@encoding": "UTF-8",
"@standalone": "yes"
},
"Entities": {
"@TotalResults": "64",
"Entity": [{
"@Type": "test-set",
"ChildrenCount": {
"Value": "0"
},
"Fields": {
"Field": [{
"@Name": "name",
"Value": "default"
},
{
"@Name": "id",
"Value": "0"
}]
},
"RelatedEntities": null
},
{
"@Type": "test-set",
"ChildrenCount": {
"Value": "0"
},
.........
.................................................. ......
但是,我想忽略 '@' 和 { “?xml”:{ "@version": "1.0", "@encoding": "UTF-8", “@standalone”:“是” },
来自输出 json 的一部分。
【问题讨论】:
标签: c# json xml serialization