C#之json字符串转xml字符串
留爪参考
using System.Xml; //
using System.Text; //
using System.Runtime.Serialization.Json; //JsonReaderWriterFactory
//以下method引用以上using
/// <summary>
/// Json字符串转xml字符串(utf-8)
/// </summary>
/// <param name="json">json字符串</param>
/// <returns>xml字符串</returns>
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
}