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的相互转换的更多相关文章

  1. Json与bean的相互转换

    本文使用json-lib jar包实现Json与bean的相互转换 1.将字符串转为JSON 使用JSONObject.fromObject(str)方法即可将字符串转为JSON对象 使用JSONOb ...

  2. json和string 之间的相互转换

    json和string 之间的相互转换 <script type="text/javascript"> //先认识一下js中json function showInfo ...

  3. 通过JDOM实现XML与String的相互转换

    利用JDOM实现XML与String之间的相互转换: package com.util.xml; import java.io.ByteArrayOutputStream; import java.i ...

  4. JSON的String字符串与Java的List列表对象的相互转换

    1.JSON的String字符串与Java的List列表对象的相互转换 在前端: 1.如果json是List对象转换的,可以直接遍历json,读取数据. 2.如果是需要把前端的List对象转换为jso ...

  5. JSON对象和字符串之间的相互转换JSON.stringify(obj)和JSON.parse(string)

    在Firefox,chrome,opera,safari,ie9,ie8等高级浏览器直接可以用JSON对象的stringify()和parse()方法. JSON.stringify(obj)将JSO ...

  6. JSON对象和string的相互转换

    JSON.stringify(obj) 将JSON转为字符串. JSON.parse(string) 将字符串转为JSON格式.

  7. 小tips:JSON对象和字符串之间的相互转换JSON.stringify(obj)和JSON.parse(string)

    在Firefox,chrome,opera,safari,ie9,ie8等高级浏览器直接可以用JSON对象的stringify()和parse()方法. JSON.stringify(obj)将JSO ...

  8. C# 序列化详解,xml序列化,json序列化对比

    本文讲讲一些纯技术的东西.并且讲讲一些原理性的东西,和一般的百度的文章不一致,如果你对序列化不清楚,绝对可以很有收获. 技术支持QQ群(主要面向工业软件及HSL组件的):592132877  (组件的 ...

  9. js压缩xml字符串,将xml字符串转换为xml对象,将xml对象转换为json对象

    /** * 压缩xml字符串 */ function compressXmlStr(str){ var prefix, suffix; var i = str.indexOf("\r&quo ...

随机推荐

  1. 【和我一起学python吧】初学Python,版本如何选择?

    早在四年多以前,在我进入英才网之前,去面试过一家海归创业的公司.他们需要的是有unix开发经验的技术人员,但是因为他们当时所处的阶段对很多成熟 技术人员不是很吸引,所以条件放宽为熟悉面向对象的程序开发 ...

  2. Dictionary<实体,List<实体>>的比较

    当Dictionary中Key为实体时,进行用ContainsKey比较会发现,就算Model为一样但是结果比较为不存在: 故用以下代码即可,现将Keys转换ToArray(),再用数组的Contai ...

  3. homework-01 最大子串和

    题目描述 对于一个给定的数列,求该数列最大的子串和(连续) 问题分析 处理发生区间上的问题时,经常会用一个非常简单经典的思路——部分和(也有叫前缀和).部分和的思想在很多复杂的区间上的算法中都有应用, ...

  4. 学习使用Markdown标记语言

    学习如何使用Markdown进行文本编辑 使用教程   大家若是经常逛Github,就知道其中有一个文件叫做README.MD.我一开始也不知道这个.MD是什么意思,后来我自己写了一次,就知道了这一种 ...

  5. protobuf 作为配置文件

    公司每个project代码中,都有一个Config类,作为模块启动的配置.其实现如下 struct Config { int num; char * file_name; int load_from_ ...

  6. JSF 2 graphicImage example

    In JSF, you can use <h:graphicImage /> tag to render a HTML "img" element. For examp ...

  7. 解决iPhone上select时常失去焦点,随意跳到下一个输入框,影响用户操作

    window.addEventListener('load', function() { FastClick.attach(document.body); }, false); //300s延迟,解决 ...

  8. 多浏览器兼容flv视频播放HTML

    HTML: <object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http: ...

  9. powershell里添加对git的支持

    在powershell命令行里依次运行 1. (new-object Net.WebClient).DownloadString("http://psget.net/GetPsGet.ps1 ...

  10. SQL Select count(*)和Count(1)的区别和执行方式及SQL性能优化

    SQL性能优化:http://www.cnblogs.com/CareySon/category/360333.html Select count(*)和Count(1)的区别和执行方式 在SQL S ...