先添加 System.ServiceModel 和System.ServiceModel.Web的引用;
然后 using System.Runtime.Serialization.Json;
方法:
public string JsonToXml(string json)
{
string xml = string.Empty;
XmlDocument xmlDoc = new XmlDocument();
try
{
XmlDictionaryReader xmlReader = JsonReaderWriterFactory.CreateJsonReader(Encoding.UTF8.GetBytes(json), XmlDictionaryReaderQuotas.Max);
xmlDoc.Load(xmlReader);
//json转xml
XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "yes");
//创建xml声明
xmlDoc.InsertBefore(xmlDec, xmlDoc.DocumentElement); //插入xml声明
//xmlDoc.AppendChild(xmlDec);
//添加xml声明
}
catch (Exception ex)
{
//
}
return xmlDoc.OuterXml; //xml转string
}