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 = ; 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);
}
}
XML 之 与Json或String的相互转换的更多相关文章
- Json与bean的相互转换
本文使用json-lib jar包实现Json与bean的相互转换 1.将字符串转为JSON 使用JSONObject.fromObject(str)方法即可将字符串转为JSON对象 使用JSONOb ...
- json和string 之间的相互转换
json和string 之间的相互转换 <script type="text/javascript"> //先认识一下js中json function showInfo ...
- 通过JDOM实现XML与String的相互转换
利用JDOM实现XML与String之间的相互转换: package com.util.xml; import java.io.ByteArrayOutputStream; import java.i ...
- JSON的String字符串与Java的List列表对象的相互转换
1.JSON的String字符串与Java的List列表对象的相互转换 在前端: 1.如果json是List对象转换的,可以直接遍历json,读取数据. 2.如果是需要把前端的List对象转换为jso ...
- JSON对象和字符串之间的相互转换JSON.stringify(obj)和JSON.parse(string)
在Firefox,chrome,opera,safari,ie9,ie8等高级浏览器直接可以用JSON对象的stringify()和parse()方法. JSON.stringify(obj)将JSO ...
- JSON对象和string的相互转换
JSON.stringify(obj) 将JSON转为字符串. JSON.parse(string) 将字符串转为JSON格式.
- 小tips:JSON对象和字符串之间的相互转换JSON.stringify(obj)和JSON.parse(string)
在Firefox,chrome,opera,safari,ie9,ie8等高级浏览器直接可以用JSON对象的stringify()和parse()方法. JSON.stringify(obj)将JSO ...
- C# 序列化详解,xml序列化,json序列化对比
本文讲讲一些纯技术的东西.并且讲讲一些原理性的东西,和一般的百度的文章不一致,如果你对序列化不清楚,绝对可以很有收获. 技术支持QQ群(主要面向工业软件及HSL组件的):592132877 (组件的 ...
- js压缩xml字符串,将xml字符串转换为xml对象,将xml对象转换为json对象
/** * 压缩xml字符串 */ function compressXmlStr(str){ var prefix, suffix; var i = str.indexOf("\r&quo ...
随机推荐
- android判断当前网络状态及跳转到设置界面
今天,想做这个跳转到网络设置界面, 刚开始用 intent = new Intent(Settings.ACTION_WIRELESS_SETTINGS); 不料老是出现settings.Wirele ...
- C++中的重载,隐藏,覆盖,虚函数,多态浅析
直到今日,才发现自己对重载的认识长时间以来都是错误的.幸亏现在得以纠正,真的是恐怖万分,雷人至极.一直以来,我认为重载可以发生在基类和派生类之间,例如: class A { public: void ...
- struts2中访问和添加Application、session以及request属性
一.访问或添加Application.session.request属性 <一>方式一 HelloWorldAction类中添加如下代码 //此方法适用于仅对Application.ses ...
- 分布式文件系统-HDFS
HDFS Hadoop的核心就是HDFS与MapReduce.那么HDFS又是基于GFS的设计理念搞出来的. HDFS全称是Hadoop Distributed System.HDFS是为以流的方式存 ...
- 并行开发——Parallel的使用 -摘自网络
随着多核时代的到来,并行开发越来越展示出它的强大威力,像我们这样的码农再也不用过多的关注底层线程的实现和手工控制, 要了解并行开发,需要先了解下两个概念:“硬件线程”和“软件线程”. 1. 硬件线程 ...
- JEE , EJB概念深入概括
说起EJB,不得不提JEE,java EE 英文全称为:java Enterprise Edition企业级应用的软件架构,是一种思想,也是一种规范,方便从事这方面的开发者以及开发厂商进行规范性的开发 ...
- Innodb刷脏页技术深度挖掘
DBA某数据库集群每日17:00左右会出现一个性能陡降的现象,在10~20秒内主库出现大量慢查询.这些查询本身没有性能问题,也没有任何关联,可以认为是由于数据库系统负载较重,由于并发导致的慢查询.通过 ...
- Spark的应用程序
Spark的应用程序,分为两部分:Spark driver 和 Spark executor.
- Yii CModel中rules验证规则
array( array(‘username’, ‘required’), array(‘username’, ‘length’, ‘min’=>3, ‘max’=>12), array( ...
- Java集合之ArrayList和LinkedList的实现原理以及Iterator详解
ArrayList实现可变数组的原理: 当元素超出数组内容,会产生一个新数组,将原来数组的数据复制到新数组中,再将新的元素添加到新数组中. ArrayList:是按照原数组的50%来延长,构造一个初始 ...