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. HTML之背景颜色的改变

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. Ajax Array Json 示例

    function functionName(){ var list=new Array(); $("td.classA").each(function(){ list.push($ ...

  3. iOS - 数组(NSArray)

    1. 数组的常用处理方式 //--------------------不可变数组 //1.数组的创建 NSString *s1 = @"zhangsan"; NSString *s ...

  4. iOS开发中的那些小技巧

    前言:今天在写代码的过程中遇到一个需要修改系统navigationBar的背景色,我起初用的是barTintColor去修改但是防不住系统点击按钮的时候会有一个渲染高亮的效果,调了好久没有达到自己想要 ...

  5. Cocos移植到Android的一些问题-SQLite3数据库移植问题

    首选我们讨论一下SQLite3数据库移植问题.我们在第14章节介绍了在Win32平台使用SQLite3数据库,我们介绍了两种配置环境的方法:一种是使用Cocos2d-x提供的SQLite3库配置,另一 ...

  6. Ueditor 1.4.3 单独调用上传图片,或文件功能

    第一步, 引入文件 <script src="ueditor/ueditor.config.js" type="text/javascript" char ...

  7. 方法:一个简单的读取配置文件.properties的工具类 JAVA

    import java.util.ResourceBundle; public class ConfigHelper { private static ConfigHelper instance; p ...

  8. 杭电ACM2076--夹角有多大(题目已修改,注意读题)

    杭电ACM2076--夹角有多大(题目已修改,注意读题) http://acm.hdu.edu.cn/showproblem.php?pid=2076 思路很简单.直接贴代码.过程分析有点耗时间. / ...

  9. C++11智能指针

    今晚跟同学谈了一下智能指针,突然想要看一下C++11的智能指针的实现,因此下了这篇博文. 以下代码出自于VS2012 <memory> template<class _Ty> ...

  10. 判断不在Update Task中

    CALL FUNCTION 'TH_IN_UPDATE_TASK'   IMPORTING     IN_UPDATE_TASK = IN_UPDATE_TASK.  "0 then not ...