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