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

  1. REST服务使用@RestController实例,输出xml/json

    REST服务使用@RestController实例,输出xml/json 需要用到的服务注解 org.springframework.web.bind.annotation.RestControlle ...

  2. Xml,Json,Hessian,Protocol Buffers序列化对比

    简介 这篇博客主要对Xml,Json,Hessian,Protocol Buffers的序列化和反序列化性能进行对比,Xml和Json的基本概念就不说了. Hessian:Hessian是一个轻量级的 ...

  3. xml json protobuf

    本文为原创,转载请注明:http://www.cnblogs.com/gistao/ Background xml,json,protobuf都是格式化手段,喜欢哪个,会用哪个,该用哪个,用哪个? 随 ...

  4. iOS开发笔记3:XML/JSON数据解析

    这篇主要总结在iOS开发中XML/JSON数据解析过程用到的方法.XML数据解析主要使用SAX方式的NSXMLParser以及DOM方式的GDataXML,JSON数据解析主要使用NSJSONSeri ...

  5. Silverlight项目笔记7:xml/json数据解析、TreeView、引用类型与数据绑定错误、图片加载、虚拟目录设置、silverlight安全机制引发的问题、WebClient缓存问题

    1.xml/json数据解析 (1)xml数据解析 使用WebClient获取数据,获取到的数据实例化为一个XDocument,使用XDocument的Descendants(XName)方法获得对应 ...

  6. [WEB API] CLIENT 指定请求及回应格式(XML/JSON)

    [Web API] Client 指定请求及响应格式(xml/json) Web API 支持的格式请参考 http://www.asp.net/web-api/overview/formats-an ...

  7. windows phone8.1:Xml,Json序列化和反序列化

    原文:windows phone8.1:Xml,Json序列化和反序列化 小梦本例主要实现以下四点内容: 将Car对象序列化为xml 将Car对象序列化为Json 将xml反序列化为Car对象 将js ...

  8. iOS基础 - XML & JSON

    一.HTML & XML HTML 是用来描述网页的一种语言 HTML 指的是超文本标记语言 (Hyper Text Markup Language) HTML 不是一种编程语言,而是一种标记 ...

  9. php中 xml json 数组 之间相互转换

    php中 xml json  数组 之间相互转换 1 数组转json $result = array( 'status' =>$status, 'message'=>$message, ' ...

随机推荐

  1. CoreAnimation 核心动画一 (一些常用属性 和 方法)

    1.常用属性: frame   bounds   center   alpha    Transition 过渡    transform 动画效果 2.常用方法: +(void)setAnimati ...

  2. Xcode6 模拟器不显示键盘

    在学习加法计算器时,程序运行后发现点击模拟器上的输入框时有时候键盘可以弹出来,有时候又弹不出来. 网上查询结果只需要在模拟器的菜单中找到hardware -> keyboard -> 取消 ...

  3. dicom格式文件 界定标识符的处理

    转自:http://www.cnblogs.com/assassinx/archive/2013/05/18/3084854.html 说到底无非几个事情 :1传输语法确定 2数据元素读取 3 7fe ...

  4. ionic2 Navigation实现报错:No component factory found for "MyComponent"

    ionic2 写的代码里面,跳转的时候报了一个 No component factory found for "RechargeSucceed" recharge() { let ...

  5. 常用的HTML 标签二

    <marquee></marquee> 滚动的文字,也称"走马灯" 语法格式 <marquee 属性="属性值">内容< ...

  6. 《HTML5与CSS3基础教程》学习笔记 ——Two Day

    第七章 1.  样式表:选择器和生命块 2.  !important: 某条声明的重要程度比其他高,在末尾添加 3.  属性值:inherit;  是强制继承 4.  1em=16px; 5.  可以 ...

  7. 特殊的attribute机制

    __attribute__机制是GNU C的一大特色,可以用来设置函数,变量和数据类型的属性,下面对工作中正好用到的两个属性做下简单介绍. 1. constructor 这个属性指定函数在main函数 ...

  8. c++ string 拼接 int错误

    程序中用到字符串和int合成字符串,受java习惯的影响,直接进行了字符串与int的+操作,结果不正确.查了一下才明白问题所在,记录一下string str=”abc”+1;输出为:bc,因为”abc ...

  9. 使用kyototycoon挂载leveldb,映射内存磁盘的使用心得

    前段时间在做大数据的KV引擎应用,测试了leveldb的性能,感觉挺好的,美中不足的是他是基于磁盘读写.在我们的场景里,IO频率预计会远远超出磁盘的承受能力,并且太频繁的读取可能也会引发磁盘恶化的速度 ...

  10. unity打包android游戏部分问题总结

    一:虚拟导航栏挡到游戏按钮: 解决方案如下: 1.获取焦点的时候隐藏 虚拟导航条 Navigation bar 隐藏导航条 2.出现导航条的时候,改变游戏界面大小 Unity tidbits: cha ...