在C#程序中,创建、写入、读取XML文件的方法

 一、在C#程序中,创建、写入、读取XML文件的方法

        1、创建和读取XML文件的方法,Values为需要写入的值     

 

 1  private void WriteXML(string Values)
 2         {        
 3                //保存的XML的地址
 4                 string XMLPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\" + "文件的名称.xml";             
 5                 XmlDocument xmlDoc = new XmlDocument(); //引入using System.Xml  命名空间
 6              
 7                 //创建类型声明
 8                 xmlDoc = new XmlDocument();
 9                 XmlNode node = xmlDoc.CreateXmlDeclaration("1.0", "utf-8", "");
10                 xmlDoc.AppendChild(node);
11                 //创建父节点
12                 XmlNode root = xmlDoc.CreateElement("父节点");
13                 xmlDoc.AppendChild(root);
14                 //创建子节点,写入值
15                 node = xmlDoc.CreateNode(XmlNodeType.Element, "子节点", null);
16                 node.InnerText = Values;
17                 root.AppendChild(node);
18                 xmlDoc.Save(XMLPath);         
19         }
20 
21  

 

2、读取XML文件中存入的值方法    

   

 1 private void ReadXML()
 2         {          
 3                 XmlDocument xmlDoc = new XmlDocument();
 4                 string XMLPath = AppDomain.CurrentDomain.SetupInformation.ApplicationBase + "\\" + "文件的名称.xml";
 5                 //如果文件存在      
 6                 if (File.Exists(XMLPath))
 7                 {
 8                     xmlDoc.Load(XMLPath); //从指定的URL加载XML文档
 9                     XmlNode Values= xmlDoc.SelectSingleNode("父节点").SelectSingleNode("子节点");
10                     string str= Values.InnerText; //获取到存入的值为 string        
11                 }
12            }

 

转载于:https://www.cnblogs.com/Mo-Maek/p/10180345.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
C# XML文件读写操作源码,以及如何调用,注释详解,有任何问题请留言, 以下截取xml文件和部分调用代码段: * ++++++++++++++++++++++++++++++++++++++ <?xml version="1.0" encoding="utf-8" standalone="no"?> <!--TestPlugins的信息--> <!--DataPlugins的信息--> * ++++++++++++++++++++++++++++ xml xl = new xml(); xl.XMLWriteRootNode("info"); //XmlElement Eml1 = xl.XMLReadNode("",0); //XmlElement Eml2 = xl.XMLReadNode("DataPlugins", 1); //XmlElement Eml4 = xl.XMLReadNode("DeviceInfo", 2); // TestPlugins XmlElement testPlugins = xl.XMLCreateNode("TestPlugins", null, null); xl.XMLInsertNode("info", 0, "TestPlugins的信息", testPlugins); // FixturePlugin XmlElement fixturePlugin = xl.XMLCreateNode("TestPlugin", null, new Dictionary() { { "Type", "FixturePlugin" } }); xl.XMLInsertNode(testPlugins.LocalName, 1, null, fixturePlugin); // DUTPlugin XmlElement DUTPlugin = (XmlElement)fixturePlugin.CloneNode(true);// xl.XMLCreateNode("TestPlugin", null, new Dictionary() { { "Type", "DUTPlugin" } }); DUTPlugin.SetAttribute("Type", "DUTPlugin"); xl.XMLInsertNode(testPlugins.LocalName, 1, null, DUTPlugin); // Agilent34461APlugin XmlElement Agilent34461APlugin = xl.XMLCreateNode("TestPlugin", null, new Dictionary() { { "Type", "Agilent34461APlugin" } }); xl.XMLInsertNode(testPlugins.LocalName, 1, null, Agilent34461APlugin); // ================================== // DataPlugins XmlElement dataPlugins = xl.XMLCreateNode("DataPlugins", null, null); xl.XMLInsertNode("info", 0, "DataPlugins的信息", dataPlugins); // CSVLogPlugin XmlElement csvlogPlugin = xl.XMLCreateNode("DataPlugin", null, new Dictionary() { { "Type", "CSVLogPlugin" } }); xl.XMLInsertNode(dataPlugins.LocalName, 1, null, csvlogPlugin); XmlElement uartlogPlugin = (XmlElement)csvlogPlugin.CloneNode(true); uartlogPlugin.SetAttribute("Type", "UartLogPlugin"); xl.XMLInsertNode(dataPlugins.LocalName, 1, null, uartlogPlugin); XmlElement testlogPlugin = (XmlElement)csvlogPlugin.CloneNode(true); testlogPlugin.SetAttribute("Type", "TestLogPlugin"); xl.XMLInsertNode(dataPlugins.LocalName, 1, null, testlogPlugin); }
C#操作XML文件需要使用System.Xml命名空间下的类。 以下是一个简单的示例,演示如何将数据写入XML文件并从读取数据: ```csharp using System; using System.Xml; class Program { static void Main(string[] args) { // 创建XML文档对象 XmlDocument xmlDoc = new XmlDocument(); // 创建XML声明 XmlDeclaration xmlDec = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null); // 添加XML声明到XML文档 xmlDoc.AppendChild(xmlDec); // 创建根元素 XmlElement root = xmlDoc.CreateElement("students"); // 将根元素添加到XML文档 xmlDoc.AppendChild(root); // 创建子元素 XmlElement student = xmlDoc.CreateElement("student"); XmlAttribute id = xmlDoc.CreateAttribute("id"); id.Value = "001"; student.Attributes.Append(id); XmlElement name = xmlDoc.CreateElement("name"); name.InnerText = "Tom"; student.AppendChild(name); XmlElement age = xmlDoc.CreateElement("age"); age.InnerText = "18"; student.AppendChild(age); // 将子元素添加到根元素 root.AppendChild(student); // 保存XML文档 xmlDoc.Save("students.xml"); // 读取XML文档 xmlDoc.Load("students.xml"); // 获取根元素 root = xmlDoc.DocumentElement; // 遍历子元素 foreach (XmlElement ele in root.ChildNodes) { Console.WriteLine("Student ID: " + ele.Attributes["id"].Value); Console.WriteLine("Name: " + ele["name"].InnerText); Console.WriteLine("Age: " + ele["age"].InnerText); } Console.ReadLine(); } } ``` 上述代码将创建一个名为“students.xml”的XML文件,其包含一个名为“students”的根元素和一个名为“student”的子元素。程序会将数据写入该文件,然后从文件读取数据并将其打印出来。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值