JSON.NET:JSON 转换为 XML ,XML 转换为 JSON

前端之家收集整理的这篇文章主要介绍了JSON.NET:JSON 转换为 XML ,XML 转换为 JSON前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

[代码]XML TO JSON

01 stringxml =@"<?xml version=""1.0"" standalone=""no""?>
02 <root>
03 <person id=""1"">
04 <name>Alan</name>
05 <url>http://www.google.com</url>
06 </person>
07 <person id=""2"">
08 <name>Louis</name>
09 <url>http://www.yahoo.com</url>
10 11 </root>";
12
13 XmlDocument doc =newXmlDocument();
14 doc.LoadXml(xml);
15
16 jsonText = JsonConvert.SerializeXmlNode(doc);
17 //{
18 @H_404_227@// "?xml": {
@H_404_227@// "@version": "1.0",
19
20 @H_404_227@// "@standalone": "no"
@H_404_227@// },monospace!important; font-size:10pt!important; min-height:inherit!important; display:block!important">22 @H_404_227@// "root": {
21
@H_404_227@// "person": [
23
24 @H_404_227@// {
@H_404_227@// "@id": "1",monospace!important; font-size:10pt!important; min-height:inherit!important; display:block!important">26 @H_404_227@// "name": "Alan",
25
@H_404_227@// "url": "http://www.google.com"
27
28 29 @H_404_227@// {
30 @H_404_227@// "@id": "2",monospace!important; font-size:10pt!important; min-height:inherit!important; display:block!important">31 @H_404_227@// "name": "Louis",monospace!important; font-size:10pt!important; min-height:inherit!important; display:block!important">32 @H_404_227@// "url": "http://www.yahoo.com"
@H_404_227@// }
33
34 @H_404_227@// ]
@H_404_227@//}
35 36
@H_234_403@ [代码]JSON TO XML
json =@"{
""?xml"": {
""@version"": ""1.0"",monospace!important; font-size:10pt!important; min-height:inherit!important; color:blue!important">""@standalone"": ""no""
},monospace!important; font-size:10pt!important; min-height:inherit!important; color:blue!important">""root"": {
""person"": [
{
""@id"": ""1"",monospace!important; font-size:10pt!important; min-height:inherit!important; color:blue!important">""name"": ""Alan"",monospace!important; font-size:10pt!important; min-height:inherit!important; color:blue!important">""url"": ""http://www.google.com""
{
""@id"": ""2"",monospace!important; font-size:10pt!important; min-height:inherit!important; color:blue!important">""name"": ""Louis"",monospace!important; font-size:10pt!important; min-height:inherit!important; color:blue!important">""url"": ""http://www.yahoo.com""
}
]
}";
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
// <?xml version="1.0" standalone="no"?>
// <root>
// <person id="1">
// <name>Alan</name>
// <url>http://www.google.com</url>
// </person>
// <person id="2">
// <name>Louis</name>
// <url>http://www.yahoo.com</url>
// </root>
@H_234_403@ [代码]DEMO:JSON TO XML
json_str ="{\"a\":\"a\",\"b\":\"b\"}"//json 的字符串需要按照这个格式 书写,否则会报错
""root"":"+ json_str + "}";
if(!.IsNullOrEmpty(json))
{
XmlDocument doc = JsonConvert.DeserializeXmlNode(json);
}

猜你在找的Json相关文章