xml的问题 XmlDocument 与json转换

2021-01-15 13:12

阅读:877

标签:job   ali   art   close   iter   toe   ntp   success   register   

XMl

//实现xml的写入
//1、在内存中构建Dom对象
XmlDocument xmlDoc = new XmlDocument();
//增加文档说明
XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "GBK", null);
xmlDoc.AppendChild(xmlDeclaration);
//增加根元素
// 创建根元素
XmlElement root = xmlDoc.CreateElement("root");
xmlDoc.AppendChild(root);
//3、增加子元素,接下来添加的子元素增加到rootElement节点下
XmlElement head = xmlDoc.CreateElement("head");
root.AppendChild(head);
//4、为head创建节点。
XmlElement xorgan = xmlDoc.CreateElement("organ");

root.AppendChild(vehispara);

 

/// 
        /// 将XmlDocument转化为string
        /// 
        /// 
        /// 
        public static string ConvertXmlToString(XmlDocument xmlDoc)
        {
            MemoryStream stream = new MemoryStream();
            XmlTextWriter writer = new XmlTextWriter(stream, null);
            writer.Formatting = Formatting.Indented;
            xmlDoc.Save(writer);
            StreamReader sr = new StreamReader(stream, System.Text.Encoding.UTF8);
            stream.Position = 0;
            string xmlString = sr.ReadToEnd();
            sr.Close();
            stream.Close();
            return xmlString;
        }

 

 

将字符转换为XML格式的并且提取其中的内容。

XmlDataDocument xm = new XmlDataDocument();
            xm.LoadXml(b);
            xm.Save("haha");
            XmlNode node = xm.SelectSingleNode("root/head/code");
            string code = node.InnerText;
            node = xm.SelectSingleNode("root/head/message");
            string message = node.InnerText;

  

 

问题: 这样的类如何改为强类型。

2-GA013533
5881郭宝山火龙镇18749597210叉车CPC35H-2AA4AF19653杭叉集团股份有限公司0002020-07-2711C490BPG15163016浙江新柴股份有限公司B020-1240.026502011-06-304110200701200727000055

  怎么办了? 为了读取方便我创建了相关的类。

      试了好多种方法,什么序列号,反序列都不是太好用, 实在没办法,直接转json 用 Newtonsoft.Json; 是真好用。 

但是新问题:   string json2 = Newtonsoft.Json.JsonConvert.SerializeXmlNode(xn);

转换的json2 = "{"vehispara":{"@id":"0","license":"2-GA013533","licensetype":"5","licensecode":"88","vehicletype":"1","owner":"郭宝山","lxdz":"火龙镇","lxdh":"18749597210","cpmc":"叉车","vehiclemodel":"CPC35H-2","usetype":"A","vin":"A4AF19653","clscqy":"杭叉集团股份有限公司","gvm":"0","zbzl":"0","rm":"0","registerdate":"2020-07-27","drivemode":"1","gear":"1","engine":"C490BPG","fdjh":"15163016","enginemanuf":"浙江新柴股份有限公司","fueltype":"B","odometer":"0","airin":"2","fuelsupply":"0","rygg":"","ssxq":"-1","standard":"2","enginepower":"40.0","enginespeed":"2650","mdate":"2011-06-30","jylsh":"411020070120072700005","testtimes":"5"}}"

因为他包了一层 有vehispara 他还不能直接用。

需要直接获取 string  

看了这篇文章用了 

https://www.cnblogs.com/sky6699/p/6889762.html

string json2 = Newtonsoft.Json.JsonConvert.SerializeXmlNode(xn);
JObject obj = Newtonsoft.Json.Linq.JObject.Parse(json2); 

这两个语句解决问题。 

在对xml进行操作时,InnerText和innerXml区别

同样插入匿名,

使用innerText插入的结果是:<authorName>匿名</authorName>

使用innerXml插入的结果是:匿名

使用text会被转意。 

 

DataSet  这个需要再了解一下,因为XmlDocument 已经被弃用。 

json 转字典 Dictionary

 JavaScriptSerializer s = new JavaScriptSerializer();

string jsonTexts = "{\"count\":\"5550\",\"status\": \"200\",\"message\": \"success\",\"show_data\":[{\"productsId\":\"10025\",\"productName\":\"粉色波点裙蕾丝帽子熊 00190009\",\"discountPrice\":\"98.00\",\"defaultPrice\":\"null\",\"frontImg_160_160\":\"技术图片http://img.chaotianmen.com/upload/productimg/20140310/1403101454563877026_160_160.jpg\",\"frontImg\":\"http://img.chaotianmen.com/upload/productimg/20140310/1403101454563877026.jpg\",\"totalSales\":\"1\",\"shopName\":\"喜洋洋精品儿童玩具专卖店\"},{\"productsId\":\"10026\",\"productName\":\"粉色波点裙蕾丝帽子熊 00190009\",\"discountPrice\":\"98.00\",\"defaultPrice\":\"null\",\"frontImg_160_160\":\"技术图片http://img.chaotianmen.com/upload/productimg/20140310/1403101454563877026_160_160.jpg\",\"frontImg\":\"http://img.chaotianmen.com/upload/productimg/20140310/1403101454563877026.jpg\",\"totalSales\":\"1\",\"shopName\":\"喜洋洋精品儿童玩具专卖店\"}]}"; 

Dictionary JsonData = (Dictionary)s.DeserializeObject(jsonTexts);
 
 
 
 

xml的问题 XmlDocument 与json转换

标签:job   ali   art   close   iter   toe   ntp   success   register   

原文地址:https://www.cnblogs.com/zzz7/p/13391315.html


评论


亲,登录后才可以留言!