xml转换为json格式时,如何将指定节点转换成数组

前端之家收集整理的这篇文章主要介绍了xml转换为json格式时,如何将指定节点转换成数组前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

【json.net】xml转换为json格式时,如何将指定节点转换成数组


[已解决问题] 浏览: 2242次
1 using System.Xml.Linq;
2 using Newtonsoft.Json;
3 
4 
5 Response.ContentType = "application/json";
6 
7 XDocument xdoc = XDocument.Load(path);
8 
9 Response.Write(JsonConvert.SerializeXNode(xdoc));

xml 片段 :

<specialty nameCN="电测">
        <step>
            <signer staffID="800706" nameCN=""><![CDATA[]]></signer>
        </step>
        <step>
            <signer staffID="090477" nameCN=""><![CDATA[]]></signer>
        </step>
    </specialty>
    <specialty nameCN="节能">
        <step>
            <signer staffID="800608" nameCN=""><![CDATA[]]></signer>
            <signer staffID="800808" nameCN=""><![CDATA[]]></signer>
        </step>
        <step>
            <signer staffID="800602" nameCN=""><![CDATA[]]></signer>
            <signer staffID="800803" nameCN=""><![CDATA[]]></signer>
        </step>
    </specialty>

//输出 json 结果

{
  "@nameCN": "电测","step": [
    {
      "signer": {
        "@staffID": "800706","@nameCN": "","#cdata-section":  }
    },{
      "signer": {
        "@staffID": "090477","#cdata-section":  }
    }
  ]
},{
  "@nameCN": "节能","step": [
    {
      "signer": [ { "@staffID": "800608","#cdata-section": },{ "@staffID": "800808","#cdata-section": } ]
    },{
      "signer": [ { "@staffID": "800602",{ "@staffID": "800803","#cdata-section": } ]
    }
  ]
}

上面的结果 用红色标记出来的就是差别,step下有多个signer节点时,输出结果signer是数组

只有1个signer节点 输出signer不是数组如何在只有一个signer节点时也输出为数组

猜你在找的XML相关文章