写XML文件

本文介绍了一种使用C#进行XML配置文件读写的方法,包括设置不同类型的值(如字符串、整数、布尔值和日期时间),创建新的XML文档,并删除节点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

     public class WriteXML
    {
        public static void SetXMLValue(string AppKey, string AppValue)
        {
            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(Application.StartupPath + "//" + Application.ProductName + ".exe.config");
                XmlNode xNode;
                XmlElement xElem1;
                xNode = xDoc.SelectSingleNode("//userSettings");
                xElem1 = (XmlElement)xNode.SelectSingleNode("//setting[@name='" + AppKey + "']").LastChild;
                if (xElem1 != null)
                    xElem1.InnerXml = AppValue;
                xDoc.Save(Application.StartupPath + "//" + Application.ProductName + ".exe.config");
            }
            catch
            {

            }
        }

        public static void SetXMLValue(string AppKey, int AppValue)
        {
            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(Application.StartupPath + "//" + Application.ProductName + ".exe.config");
                XmlNode xNode;
                XmlElement xElem1;
                xNode = xDoc.SelectSingleNode("//userSettings");
                xElem1 = (XmlElement)xNode.SelectSingleNode("//setting[@name='" + AppKey + "']").LastChild;
                if (xElem1 != null)
                    xElem1.InnerXml = AppValue.ToString();
                xDoc.Save(Application.StartupPath + "//" + Application.ProductName + ".exe.config");
            }
            catch
            {

            }
        }

        public static void SetXMLValue(string AppKey, bool AppValue)
        {
            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(Application.StartupPath + "//" + Application.ProductName + ".exe.config");
                XmlNode xNode;
                XmlElement xElem1;
                xNode = xDoc.SelectSingleNode("//userSettings");
                xElem1 = (XmlElement)xNode.SelectSingleNode("//setting[@name='" + AppKey + "']").LastChild;
                if (xElem1 != null)
                    xElem1.InnerXml = AppValue.ToString();
                xDoc.Save(Application.StartupPath + "//" + Application.ProductName + ".exe.config");
            }
            catch
            {

            }
        }

        public static void SetXMLValue(string AppKey, DateTime AppValue)
        {
            try
            {
                XmlDocument xDoc = new XmlDocument();
                xDoc.Load(Application.StartupPath + "//" + Application.ProductName + ".exe.config");
                XmlNode xNode;
                XmlElement xElem1;
                xNode = xDoc.SelectSingleNode("//userSettings");
                xElem1 = (XmlElement)xNode.SelectSingleNode("//setting[@name='" + AppKey + "']").LastChild;
                if (xElem1 != null)
                    xElem1.InnerXml = AppValue.ToString();
                xDoc.Save(Application.StartupPath + "//" + Application.ProductName + ".exe.config");
            }
            catch
            {

            }
        }

        public static void CreateXMLDocument(string MyLoveMisc, string miscName)
        {
            XmlDocument xmldoc;
            XmlNode xmlnode;
            XmlElement xmlelem;
            XmlElement MiscList;

            XmlText xmltext;
            if (System.IO.File.Exists(Application.StartupPath + "//" + MyLoveMisc + ".xml"))
            {
                xmldoc = new XmlDocument();
                xmldoc.Load(Application.StartupPath + "//" + MyLoveMisc + ".xml");
                MiscList = xmldoc.CreateElement("MiscList");
                MiscList = xmldoc.CreateElement("", "MiscList", "");
                xmltext = xmldoc.CreateTextNode(miscName);
                MiscList.AppendChild(xmltext);
                xmldoc.ChildNodes.Item(1).AppendChild(MiscList);
            }
            else
            {
                xmldoc = new XmlDocument();
                //加入XML的声明段落
                xmlnode = xmldoc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
                xmldoc.AppendChild(xmlnode);
                //加入一个根元素
                xmlelem = xmldoc.CreateElement("", "MyLoveMisc", "");
                //  xmltext = xmldoc.CreateTextNode("heyu5220");
                //  xmlelem.AppendChild(xmltext);
                xmldoc.AppendChild(xmlelem);
                //加入另外一个元素
                MiscList = xmldoc.CreateElement("MiscList");
                MiscList = xmldoc.CreateElement("", "MiscList", "");
                xmltext = xmldoc.CreateTextNode(miscName);
                MiscList.AppendChild(xmltext);
                xmldoc.ChildNodes.Item(1).AppendChild(MiscList);
            }
            //保存创建好的XML文档
            try
            {
                xmldoc.Save(Application.StartupPath + "//" + MyLoveMisc + ".xml");
            }
            catch (Exception e)
            {
                //显示错误信息
                Console.WriteLine(e.Message);
            }
            Console.ReadLine();
        }

        public static void DeleteXMLNode(string MyLoveMisc, string miscName)
        {
            if (System.IO.File.Exists(Application.StartupPath + "//" + MyLoveMisc + ".xml"))
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(Application.StartupPath + "//" + MyLoveMisc + ".xml");
                XmlNodeReader reader = new XmlNodeReader(doc["MyLoveMisc"]);

                XmlNodeList xnl = doc["MyLoveMisc"].ChildNodes;
                foreach (XmlNode xn in xnl)
                {
                    XmlElement xe = (XmlElement)xn;
                    if (xe.InnerText == miscName)
                    {
                        doc["MyLoveMisc"].RemoveChild(xn);
                        doc.Save(Application.StartupPath + "//" + MyLoveMisc + ".xml");
                        break;
                    }
                }
            }
            else
            {
            }
        }
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值