【问题标题】:Covert XML string to json将 XML 字符串转换为 json
【发布时间】: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


【解决方案1】:

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

doc.RemoveChild(doc.FirstChild);

这应该可以解决问题。

【讨论】:

    猜你喜欢
    • 2012-10-26
    • 2016-03-17
    • 2019-10-29
    • 2021-09-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-04
    • 2018-08-29
    相关资源
    最近更新 更多