加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-16 19:27:26 所属栏目:百科 来源:网络整理
导读:[代码] XML TO JSON 01 string xml = @"?xml version=""1.0"" standalone=""no""? 02 root 03 person id=""1"" 04 nameAlan/name 05 urlhttp://www.google.com/url 06 /person 07 person id=""2"" 08 nameLouis/name 09 urlhttp://www.yahoo.com/url 10 11 /

[代码]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 // "?xml": {
19 // "@version": "1.0",
20 // "@standalone": "no"
21 // },monospace!important; font-size:10pt!important; min-height:inherit!important; display:block!important">22 // "root": {
23 // "person": [
24 // {
25 // "@id": "1",monospace!important; font-size:10pt!important; min-height:inherit!important; display:block!important">26 // "name": "Alan",
27 // "url": "http://www.google.com"
28 29 // {
30 // "@id": "2",monospace!important; font-size:10pt!important; min-height:inherit!important; display:block!important">31 // "name": "Louis",monospace!important; font-size:10pt!important; min-height:inherit!important; display:block!important">32 // "url": "http://www.yahoo.com"
33 // }
34 // ]
35 36 //}
[代码]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>
[代码]DEMO:JSON TO XML
view source
print ?
json_str ="{"a":"a","b":"b"}"//json 的字符串需要按照这个格式 书写,否则会报错
""root"":"+ json_str + "}";
if(!.IsNullOrEmpty(json))
{
XmlDocument doc = JsonConvert.DeserializeXmlNode(json);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

相关内容
推荐文章
站长推荐
热点阅读