隐藏XML字符串到json

时间:2018-12-04 03:53:21

标签: c# json xml serialization

我有一个字符串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。

1 个答案:

答案 0 :(得分:0)

您可以简单地从XML文档中删除第一个孩子,然后再将其序列化为json。看起来像这样。

doc.RemoveChild(doc.FirstChild);

这应该可以解决问题。