using System.Xml.Linq;
using Newtonsoft.Json; Response.ContentType = "application/json";
XDocument xdoc = XDocument.Load(path);
Response.Write(JsonConvert.SerializeXNode(xdoc));
xml 片段 :
<specialty nameCN="电测">
<step>
<signer staffID="" nameCN=""><![CDATA[]]></signer>
</step>
<step>
<signer staffID="" nameCN=""><![CDATA[]]></signer>
</step>
</specialty>
<specialty nameCN="节能">
<step>
<signer staffID="" nameCN=""><![CDATA[]]></signer>
<signer staffID="" nameCN=""><![CDATA[]]></signer>
</step>
<step>
<signer staffID="" nameCN=""><![CDATA[]]></signer>
<signer staffID="" nameCN=""><![CDATA[]]></signer>
</step>
</specialty>
输出 json 结果:
{
"@nameCN": "电测",
"step": [
{
"signer": {
"@staffID": "",
"@nameCN": "",
"#cdata-section":
}
},
{
"signer": {
"@staffID": "",
"@nameCN": "",
"#cdata-section":
}
}
]
},
{
"@nameCN": "节能",
"step": [
{
"signer": [
{
"@staffID": "",
"@nameCN": "",
"#cdata-section":
},
{
"@staffID": "",
"@nameCN": "",
"#cdata-section":
}
]
},
{
"signer": [
{
"@staffID": "",
"@nameCN": "",
"#cdata-section":
},
{
"@staffID": "",
"@nameCN": "",
"#cdata-section":
}
]
}
]
}
上面的结果 用红色标记出来的就是差别,step下有多个signer节点时,输出结果signer是数组,
只有1个signer节点 输出signer不是数组,如何在只有一个signer节点时也输出为数组