xml的问题 XmlDocument 与json转换

Posted zzz7

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了xml的问题 XmlDocument 与json转换相关的知识,希望对你有一定的参考价值。

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);

 

/// <summary>
        /// 将XmlDocument转化为string
        /// </summary>
        /// <param name="xmlDoc"></param>
        /// <returns></returns>
        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;

  

 

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

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

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

      试了好多种方法,什么序列号,反序列都不是太好用, 实在没办法,直接转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区别

同样插入<authorName>匿名</authorName>,

使用innerText插入的结果是:&lt;authorName&gt;匿名&lt;/authorName&gt;

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

使用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<string, object> JsonData = (Dictionary<string, object>)s.DeserializeObject(jsonTexts);
 
 
 
 

以上是关于xml的问题 XmlDocument 与json转换的主要内容,如果未能解决你的问题,请参考以下文章

使用 XmlDocument 读取 XML 属性

如何将 XML 文件(XmlDocument)的内容写入文本文件?

将 XmlDocument 输出为 XML

XmlDocument.Load()加载xml文件时,提示分析 EntityName 时出错的问题。

如何在 XmlDocument 中插入/替换 XML 标签?

在 C# 中使用 Xmldocument 修改 xml