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. HDU4277 USACO ORZ(dfs+set)

    Problem Description Like everyone, cows enjoy variety. Their current fancy is new shapes for pasture ...

  2. 【网络收集】获取JavaScript 的时间使用内置的Date函数完成

    var mydate = new Date(); mydate.getYear(); //获取当前年份(2位) mydate.getFullYear(); //获取完整的年份(4位,1970-???? ...

  3. hdu 1716(dfs)

    题目链接 : http://acm.hdu.edu.cn/showproblem.php?pid=1716     排列2   Problem Description Ray又对数字的列产生了兴趣:现 ...

  4. WCF之安全

    传输安全. 点对点,对整个消息进行加密,性能损失,当中间者不安全时,消息也就不安全了. WCF中支持传输安全和消息安全模式. 通过配置和绑定来设置.<Security Mode =Transct ...

  5. NodeJS服务器:一行代码 = 一个的HTTP服务器

    从这一篇开始,我们进入技术讲解的话题,逐步实现用NodeJS实现文件的传送共享服务. 前文我们讲过,NodeJS是最擅长做网络服务器的,今天我们就来用NodeJS做一个最简单的服务器. 先看一幅图: ...

  6. java虚拟机理解探索1

    以下内容源于个人对<深入java虚拟机>的理解总结 基本概念: java虚拟机可以指一种抽象规范,也可以指一种具体实现,亦可以指一个java虚拟机实例. 虚拟机生命周期: 一个java虚拟 ...

  7. 隐藏虚拟键盘,解决键盘挡住UITextField问题

    再正式开始之前,先来介绍一下IOS的键盘类型: 一.键盘风格 UIKit框架支持8种风格键盘 ? 1 2 3 4 5 6 7 8 9 10 typedef enum {      UIKeyboard ...

  8. UIView背景渐变三种方法

    //此作品非原创 #import "ACViewController.h" @interface ACViewController () @end @implementation  ...

  9. 【Newtonsoft.Json】Window Phone Json解析开发包

    WP从服务器.API交换数据一般都是用JSON格式字符串. 下面介绍用Newtonsoft.Json来处理JSON. 准备 1.到 http://json.codeplex.com/ 下载Newton ...

  10. PCB参数计算神器-Saturn PCB Design Toolkit下载及安装指南

    进行PCB设计,特别是高频高速设计时,难免会涉及到PCB相关参数的计算及设置,如:VIA的过流能力,VIA的寄生电容.阻抗等,导线的载流能力,两相互耦合信号线间的串扰,波长等参数. 这里向大家介绍一款 ...