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 ...
随机推荐
- Blogilo:Ubuntu下“wlw”
原文首发:http://www.ido321.com/1274.html 这一篇博客是在Ubuntu的博客客户端blogilo下发布的,感觉有点高大上,特此发布一篇博文记录一下. 博客发布在 ...
- 【成都GamEver游戏公司诚邀服务器伙伴】【7~15k一年4次项目奖金】
关于我们 我们厌倦了朝九晚五,一眼看到头的人生我们厌倦了耗费自己青春做的都是没有感情的项目平均从业经验5年以上行业顶尖美术和金牌制作人,资深欧美制作经验立志做中国的suppercell,公司小而美 我 ...
- web.py处理文件上传
#coding=utf8 import web urls = ('/','Home', '/upload', 'Upload') app = web.application(urls, globals ...
- libvirt虚拟系统如何增加usb设备
之前干这些事情都是通过virt-manager来搞定的.不过由于这个图形界面不太方便,而且现在没法打开(具体原因不详,每次打开提示一些方法未实现什么的),所以试下用libvirt的命令virsh来搞定 ...
- homework-03 图形化化最大子序列和
你现在使用的代码规范是什么, 和上课前有什么改进? 我们一开始使用的是C++完成的相关程序.本次因为一些原因,改为C#进行编写.因为2013-10-21在VS2012中,所以所有的代码都已经被VS自 ...
- URAL 2040 Palindromes and Super Abilities 2 (回文自动机)
Palindromes and Super Abilities 2 题目链接: http://acm.hust.edu.cn/vjudge/contest/126823#problem/E Descr ...
- Spring AOP + AspectJ in XML configuration example
For those don't like annotation or using JDK 1.4, you can use AspectJ in XML based instead. Review l ...
- 生成Base58格式的UUID(Hibernate Base64格式的UUID续)
Base58简介 Base58采用的字符集合为“123456789abcdefghijkmnopqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ”,从这不难看出,Base58是纯数 ...
- 在Hibernate中使用HibernateTemplate来进行包含sql语句的查询
/** * 使用sql语句进行查询操作 * @param sql * @return */ public List queryWithSql(final Stri ...
- 教你50招提升ASP.NET性能(二十六):对于开发人员的数据库性能技巧
Database Performance Tips for Developers对于开发人员的数据库性能技巧 As a developer you may or may not need to go ...