C#中对xml数据的读取和写入
2021/4/16 14:55:14
本文主要是介绍C#中对xml数据的读取和写入,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
C#中对xml数据的读取和写入:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml.Linq; using System.Xml; using Newtonsoft.Json; namespace cashUI.test { class XmlTest { private static string localPath = System.Environment.CurrentDirectory + @"\test\"; /* * 写入xml */ public void writeXml() { string path = localPath + "/test.xml"; XmlDocument docment = new XmlDocument(); XmlNode node = docment.AppendChild(docment.CreateXmlDeclaration("1.0", "UTF-8", null)); XmlElement users = docment.CreateElement("Users");//一级节点 XmlElement user = docment.CreateElement("User");//二级节点 //创建三级节点 XmlElement userName = docment.CreateElement("userName"); userName.InnerText = "root111"; XmlElement passWord = docment.CreateElement("passWord"); passWord.InnerText = "123456"; //三级节点加入到二级节点中 user.AppendChild(userName); user.AppendChild(passWord); users.AppendChild(user); docment.AppendChild(users); docment.Save(path); } /* * 读取修改xml */ public void readXml() { string path = localPath + "/test.xml"; XmlDocument docment = new XmlDocument(); docment.Load(path); XmlNode rootNode = docment.SelectSingleNode("Users"); for (int i = 0; i < rootNode.ChildNodes.Count; i++) { if (rootNode.ChildNodes[i].ChildNodes[0].InnerText == "root111") { rootNode.ChildNodes[i].ChildNodes[0].InnerText = "root222"; rootNode.ChildNodes[i].ChildNodes[1].InnerText = "654321"; } } docment.Save(path); } /* * xml转成json * 把xml读取转成对象,把对象转json */ public void xml2json() { //1.读取xml的数据 string path = localPath + "/test.xml"; XmlDocument docment = new XmlDocument(); docment.Load(path); //string jsonText = JsonConvert.SerializeXmlNode(docment); //2.把读取出来的xml数据循环放进key->value字典类型中 List<Dictionary<string, string>> list = new List<Dictionary<string, string>>(); XmlNode rootNode = docment.SelectSingleNode("Users"); for (int i = 0; i < rootNode.ChildNodes.Count; i++) { Dictionary<string, string> dic = new Dictionary<string, string>(); dic.Add(rootNode.ChildNodes[i].ChildNodes[0].Name, rootNode.ChildNodes[i].ChildNodes[0].InnerText); dic.Add(rootNode.ChildNodes[i].ChildNodes[1].Name, rootNode.ChildNodes[i].ChildNodes[1].InnerText); list.Add(dic); } Dictionary<string, List<Dictionary<string, string>>> dict = new Dictionary<string, List<Dictionary<string, string>>>(); dict.Add("data", list); //3.把对象型的数据转json string jsonText = JsonConvert.SerializeObject(dict); Console.WriteLine(jsonText); //return jsonText; } } }
这篇关于C#中对xml数据的读取和写入的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-12-06使用Microsoft.Extensions.AI在.NET中生成嵌入向量
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#