用C#实现XML文件的读取

 
XML 文件格式如下:
<? xml version = "1.0"encoding="utf-8"?>
< configure >
 < config id = "path"value="D: / 新建文件夹 " />
</ configure >
 
//xml 文件所在 路径
private readonly static string xmlFilePath = @"../../DvrOut/path.xml";
  private readonly static XmlDocument document = new XmlDocument();
 
/// <summary>
        /// 取XML中文件的保存路径
        ///</summary>
        ///<returns></returns>
        public string GetPathByConfigid()
        {
            document.Load(xmlFilePath);
 
            string result = null;
            foreach (XmlNode node in document["configure"])
            {
                if (node.Attributes["id"].Value.ToString() == "path")
                {
                    result = node.Attributes["value"].Value.ToString();
                }
            }
            return result;
        }
        ///<summary>
        /// 置XML中文件的保存路径
        ///</summary>
        ///<param name="text"></param>
        public static void SetConfig(string text)
        {
            document.Load(xmlFilePath);
 
            foreach (XmlNode node in document["configure"])
            {
                if (node.Attributes["id"].Value.ToString() == "path")
                {
                    node.Attributes["value"].Value = text;
                }
            }
            document.Save(xmlFilePath);
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 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、付费专栏及课程。

余额充值