JSON goto XML

Source: Internet
Author: User

The first method:

You need to use a namespace System.Runtime.Serialization.Json

There are jsonreaderwriterfactory below.

XmlDictionaryReader reader = Jsonreaderwriterfactory.createjsonreader (Encoding.UTF8.GetBytes (Sjson), Xmldictionaryreaderquotas.max);
XmlDocument doc = new XmlDocument ();
Doc. Load (reader);

The namespace System.Web.Script.Serialization you need to use. JavaScriptSerializer

Below are JavaScriptSerializer

JSON string conversion to XML object
public static XmlDocument Json2xml (String Sjson)
{
XmlDictionaryReader reader = Jsonreaderwriterfactory.createjsonreader (Encoding.UTF8.GetBytes (Sjson), Xmldictionaryreaderquotas.max);
XmlDocument doc = new XmlDocument ();
Doc. Load (reader);

JavaScriptSerializer Oserializer = new JavaScriptSerializer ();
dictionary<string, object> Dic = (dictionary<string, object>) oserializer.deserializeobject (SJson);
XmlDocument doc = new XmlDocument ();
XmlDeclaration Xmldec;
Xmldec = doc. Createxmldeclaration ("1.0", "gb2312", "yes");
Doc. InsertBefore (Xmldec, Doc. DocumentElement);
XmlElement nroot = doc.createelement_x ("root");
Doc. AppendChild (Nroot);
foreach (keyvaluepair<string, object> item in Dic)
{
XmlElement element = doc.createelement_x (item. Key);
Keyvalue2xml (element, item);
Nroot.appendchild (Element);
}
return doc;
}

private static void Keyvalue2xml (XmlElement node, keyvaluepair<string, object> Source)
{
Object kvalue = Source.value;
if (kvalue.gettype () = = typeof (Dictionary<string, object>))
{
foreach (keyvaluepair<string, object> item in Kvalue as Dictionary<string, object>)
{
XmlElement element = node. Ownerdocument.createelement_x (item. Key);
Keyvalue2xml (element, item);
Node. AppendChild (Element);
}
}
else if (kvalue.gettype () = = typeof (object[]))
{
object[] o = kvalue as object[];
for (int i = 0; i < o.length; i++)
{
XmlElement Xitem = node. Ownerdocument.createelement_x ("Item");
keyvaluepair<string, object> item = new keyvaluepair<string, object> ("item", O[i]);
Keyvalue2xml (Xitem, item);
Node. AppendChild (Xitem);
}

}
Else
{
XmlText Text = node. Ownerdocument.createtextnode (Kvalue.tostring ());
Node. AppendChild (text);
}
}

The second method:

XML to JSON

123456789101112131415161718192021222324252627282930313233343536 string xml = @"<?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>  </person></root>"; XmlDocument doc = new XmlDocument();doc.LoadXml(xml); string jsonText = JsonConvert.SerializeXmlNode(doc);//{//  "?xml": {//    "@version": "1.0",//    "@standalone": "no"//  },//  "root": {//    "person": [//      {//        "@id": "1",//        "name": "Alan",//        "url": "http://www.google.com"//      },//      {//        "@id": "2",//        "name": "Louis",//        "url": "http://www.yahoo.com"//      }//    ]//  }//}

JSON to XML

123456789101112131415161718192021222324252627282930313233 string json = @"{  ""?xml"": {    ""@version"": ""1.0"",    ""@standalone"": ""no""  },  ""root"": {    ""person"": [      {        ""@id"": ""1"",        ""name"": ""Alan"",        ""url"": ""http://www.google.com""      },      {        ""@id"": ""2"",        ""name"": ""Louis"",        ""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>//   </person>// </root>

Demo:json to XML

1234567891011121314 string json_str = "{\"a\":\"a\",\"b\":\"b\"}";//json 的字符串需要按照这个格式 书写,否则会报错string json = @"{  ""?xml"": {    ""@version"": ""1.0"",    ""@standalone"": ""no""  },  ""root"":" + json_str + "}";if (!string.IsNullOrEmpty(json)){    XmlDocument doc = JsonConvert.DeserializeXmlNode(json);    }

JSON goto XML

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.