C# testJsonAsXMLNodeAttribute - XML& json & Collections - XmlNode, XmlElement, XmlAttribute,Dictionary,List
testJsonAsXMLNodeAttribute
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml; namespace ParseXML
{
class Program
{
static void Main(string[] args)
{
testJsonAsXMLNodeAttribute(); }
public static void testJsonAsXMLNodeAttribute()
{ string path = @"D:\testSSGeneration\XMLforTestSSGeneration.xml";
string outpath = @"D:\testSSGeneration\xmlWithFunAttr.xml"; // 1. read xml and insert a json result as a attribute of root node XmlDocument doc = new XmlDocument();
doc.Load(path); XmlNode root = doc.FirstChild;
XmlElement xe = (XmlElement)root; ;//将子节点类型转换为XmlElement类型 List<string> list = new List<string>(); list.Add("param1");
list.Add("param2"); Dictionary<string, List<string>> dic = new Dictionary<string, List<string>>();
dic.Add("ff1", list);
dic.Add("ff2", list);
//序列化对象 string jsonStr = JsonConvert.SerializeObject(dic);//将对象转换成json存储
xe.SetAttribute("Func", jsonStr); // 2. serilization to XMLFile
doc.Save(outpath);//保存 // 3. deserilization to XMLFile
XmlDocument testxmlDoc = new XmlDocument();
testxmlDoc.Load(outpath); XmlNode testroot = testxmlDoc.FirstChild;
XmlAttributeCollection attrlist = testroot.Attributes;
XmlAttribute fffattr = attrlist["Func"]; Console.WriteLine("fffattr.Value: " + fffattr.Value); // 4. 将json转换成对象
Console.WriteLine("fffattr.Value - json");
Dictionary<string, List<string>> testdic = JsonConvert.DeserializeObject<Dictionary<string, List<string>>>(fffattr.Value); foreach (KeyValuePair<string, List<string>> kvp in testdic)
{
Console.WriteLine("key={0},value={1}", kvp.Key, kvp.Value[]);
} }
C# testJsonAsXMLNodeAttribute - XML& json & Collections - XmlNode, XmlElement, XmlAttribute,Dictionary,List的更多相关文章
- REST服务使用@RestController实例,输出xml/json
REST服务使用@RestController实例,输出xml/json 需要用到的服务注解 org.springframework.web.bind.annotation.RestControlle ...
- Xml,Json,Hessian,Protocol Buffers序列化对比
简介 这篇博客主要对Xml,Json,Hessian,Protocol Buffers的序列化和反序列化性能进行对比,Xml和Json的基本概念就不说了. Hessian:Hessian是一个轻量级的 ...
- xml json protobuf
本文为原创,转载请注明:http://www.cnblogs.com/gistao/ Background xml,json,protobuf都是格式化手段,喜欢哪个,会用哪个,该用哪个,用哪个? 随 ...
- iOS开发笔记3:XML/JSON数据解析
这篇主要总结在iOS开发中XML/JSON数据解析过程用到的方法.XML数据解析主要使用SAX方式的NSXMLParser以及DOM方式的GDataXML,JSON数据解析主要使用NSJSONSeri ...
- Silverlight项目笔记7:xml/json数据解析、TreeView、引用类型与数据绑定错误、图片加载、虚拟目录设置、silverlight安全机制引发的问题、WebClient缓存问题
1.xml/json数据解析 (1)xml数据解析 使用WebClient获取数据,获取到的数据实例化为一个XDocument,使用XDocument的Descendants(XName)方法获得对应 ...
- [WEB API] CLIENT 指定请求及回应格式(XML/JSON)
[Web API] Client 指定请求及响应格式(xml/json) Web API 支持的格式请参考 http://www.asp.net/web-api/overview/formats-an ...
- windows phone8.1:Xml,Json序列化和反序列化
原文:windows phone8.1:Xml,Json序列化和反序列化 小梦本例主要实现以下四点内容: 将Car对象序列化为xml 将Car对象序列化为Json 将xml反序列化为Car对象 将js ...
- iOS基础 - XML & JSON
一.HTML & XML HTML 是用来描述网页的一种语言 HTML 指的是超文本标记语言 (Hyper Text Markup Language) HTML 不是一种编程语言,而是一种标记 ...
- php中 xml json 数组 之间相互转换
php中 xml json 数组 之间相互转换 1 数组转json $result = array( 'status' =>$status, 'message'=>$message, ' ...
随机推荐
- JSP之邮箱检验
首先创建Email类继承Serializable接口以启用其序列化功能,而序列化又是为了保持版本的兼容性,即在版本升级时反序列化仍保持对象的唯一性.序列化接口没有方法或字段,仅用于标识可序列化的语义. ...
- C#调用SSIS包及读取DataReader目标
C#调用SSIS包需要引用两个DLL .(具体位置在C盘搜索,MSDN和百度提供的路径都不太正确) Microsoft.SQLServer.ManagedDTS.dll Microsoft.SqlSe ...
- SQLServer触发器的使用
创建: create trigger trigger_name on {table_name view_name} {for After Instead of } [ insert, update,d ...
- C语言知识总结(4)
变量的作用域 C语言根据变量作用域的不同,将变量分为局部变量和全局变量 1.局部变量 1> 定义:在函数内部定义的变量,称为局部变量.形式参数也属于局部变量. 2> 作用域:局部变量只在定 ...
- div 显示与隐藏
visibility隐藏的对象还保留对象显示时所占的物理空间,display则不保留.可以保存下面的代码看看效果: 具体步骤: 代码示例: <div style="border:1px ...
- JAVA:三种集合LIST、SET、MAP
1. 集合框架介绍 我 们知道,计算机的优势在于处理大量的数据,在编程开发中,为处理大量的数据,必须具备相应的存储结构,数组可以用来存储并处理大量类型相同的数 据,但是会发现数组在应用中的限制:数组长 ...
- (转)linux查看CPU性能及工作状态的指令mpstat,vmstat,iostat,sar,top
衡量CPU性能的指标: 1,用户使用CPU的情况:CPU运行常规用户进程CPU运行niced processCPU运行实时进程 2,系统使用CPU情况:用于I/O管理:中断和驱动用于内存管理:页面交换 ...
- 用cudamat做矩阵运算的GPU加速
1. cudamat简介 cudamat是一个python语言下,利用NVIDIA的cuda sdk 进行矩阵运算加速的库.对于不熟悉cuda编程的程序员来说,这是一个非常方便的GPU加速方案.很多工 ...
- Cstring使用说明
CString::Left(intnCount)const; //从左边1开始获取前 nCount个字符 CString::Mid(intnFirst)const; //从左边第 nCount+1个字 ...
- tar 「解/壓」 包
1.tar: short read tar壓縮包文件發生了損壞,或者说不完整. 最近,在寫下載程序的時候,發生了這個問題.初步猜測文件寫完沒有關閉,沒有寫完.