第一种方法:
需要使用命名空间System.Runtime.Serialization.Json
下面有JsonReaderWriterFactory
XmlDictionaryReader reader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(sJson), XmlDictionaryReaderQuotas.Max);
XmlDocument doc = new XmlDocument();
doc.Load(reader);
需要使用的命名空间 System.Web.Script.Serialization.JavaScriptSerializer
下面有JavaScriptSerializer
// json字符串转换为Xml对象
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);
}
}
第二种方法:
XML TO JSON
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
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
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
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
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
|
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); } |
原文:http://www.cnblogs.com/1175429393wljblog/p/5550436.html