XML 之 与Json或String的相互转换

1、XML与String的相互转换

[1] XML 转为 String

//载入Xml文件
XmlDocument xdoc = new XmlDocument();
xdoc.Load("xml文件");

string xmlStr = xdoc.InnerXml;

 

[2] String 转为 XML

//载入Xml字符串
XmlDocument xdoc = new XmlDocument();
xdoc.LoadXml("xml字符串");

//保存为Xml文件
xdoc.Save("xml文件");

 

2、XML 与 Json 的相互转换

[1] XML 转换为 Json

using System.Xml;  
using Newtonsoft.Json;  

string xml = "<xml><Name>Test class</Name><X>100</X><Y>200</Y></xml>"; XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(doc);

 

[2] Json 转换为 XML 

方法一:

string json = Newtonsoft.Json.JsonConvert.SerializeXmlNode(xml);  
XmlDocument xmlDoc = Newtonsoft.Json.JsonConvert.DeserializeXmlNode(json);
xmlDoc.Save("F:/example.xml");

 

方法二:

        
    using System.Xml;
    using System.Runtime.Serialization.Json;
    using System.Web.Script.Serialization;
    /// <summary> /// json字符串转换为Xml对象 /// XmlDictionaryReader命名空间为using System.Runtime.Serialization.Json; /// JavaScriptSerializer需添加System.Web.Extensions.dll引用
    /// JavaScriptSerializer命名空间为System.Web.Script.Serialization;
/// </summary> /// <param name="sJson"></param> /// <returns></returns> 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 = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes"); doc.InsertBefore(xmlDec, doc.DocumentElement); XmlElement nRoot = doc.CreateElement("root"); doc.AppendChild(nRoot); foreach (KeyValuePair<string, object> item in Dic) { XmlElement element = doc.CreateElement(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(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("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); } }

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值