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. Part 12 DateTime functions in SQL Server

    DateTime functions in SQL Server IsDate, Day, Month, Year and DateName DateTime functions in SQL Ser ...

  2. easyui中Tab的tools按钮刷新当前tab

    easyui中Tab的tools按钮刷新当前tab 点击刷新按钮,刷新当前Tab选项卡. $('#index_tabs').tabs({ fit : true, border : false, too ...

  3. 遇到的 autoresizingMask 相关的问题

    1.前言 当一个控件设置好 frame,然后出现会 frame 显示不准或是跟随父控件的变化而变化了,你就要考虑是否是 autoresizing 的问题了 当在 xib 中布局时,报 NSAutore ...

  4. XMLHTTPRequest的属性和方法简介

    由于现在在公司负责制作标准的静态页面,为了增强客户体验,所以经常要做些AJAX效果,也学你也和我一样在,学习AJAX.而设计AJAX时使用的一个 重要的技术(工具)就是XMLHTTPRequest对象 ...

  5. jquery页面滚动,菜单固定到顶部

    // 菜单固定 $(function(){ //获取要定位元素距离浏览器顶部的距离 var navH = $("#topp").offset().top; //滚动条事件 $(wi ...

  6. spring IOC经典理解

    不多解释,直接上图片!

  7. [GeekBand] C++ 高级编程技术 (1)

    一.类型转换 class Fraction { public: explicit Fraction(int num, int den=1) : m_numerator(num), m_denomina ...

  8. Tables for condition techniques

    T682i  -- Access sequence and the tables   T685  -- Condition types and Access sequences   T683s  -- ...

  9. Linux磁盘与文件系统概念理解

    磁盘级别概念     这里讲的主要是网上所谓的老式磁盘,它是由一个个盘片组成的,我们先从个盘片结构讲起.如图1所示,图中的一圈圈灰色同心圆为一条条磁道,从圆心向外画直线,可以将磁道划分为若干个弧段,每 ...

  10. 转载:Linux内核探索之路——关于书

    转自http://blog.chinaunix.net/uid-20608849-id-3029223.html 在学习Linux内核代码的过程中,定会参考很多书籍以及网路资源,但是并不是所有的书籍和 ...