All-----------XML

本文介绍如何使用C#进行XML文件的基本操作,包括创建、添加、显示和更新XML节点。通过示例代码展示了如何利用XmlDocument类实现这些功能。

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

///////创建XML 

 private void button1_Click(object sender, EventArgs e)
        {
            string strIMEI = "11111111";
            try
            {
                //创建XmlDocument对象
                XmlDocument doc = new XmlDocument();
                //创建Xml节点
                XmlNode xmlNode = doc.CreateNode(XmlNodeType.XmlDeclaration, "", "");
                doc.AppendChild(xmlNode);
                //创建元素
                XmlElement xelement = doc.CreateElement("", "body", "");
                doc.AppendChild(xelement);

                XmlNode node = doc.SelectSingleNode("body");

                //设备元素
                XmlElement element = doc.CreateElement("item");
                element.SetAttribute("IMEI", strIMEI);
                node.AppendChild(element);

                //得到当前应用程序所在目录
                string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
                doc.Save(path + "//config.xml");
            }
            catch (Exception ea)
            {
                MessageBox.Show(ea.Message.ToString());
            }
        }

============================================

//添加节点

       private void button2_Click(object sender, EventArgs e)
        {
            //添加节点
            XmlDocument xmlDoc = new XmlDocument();
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
            xmlDoc.Load(path + @"/config.xml");
            XmlNode node = xmlDoc.SelectSingleNode("body");
            XmlElement element = xmlDoc.CreateElement("username");
            element.SetAttribute("name", "aaaaa");
            node.AppendChild(element);
            xmlDoc.Save(path + "//config.xml");
        }

=================================================

//显示节点

        private void button3_Click(object sender, EventArgs e)
        {
            //显示节点
            XmlDocument xmlDoc = new XmlDocument();
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
            xmlDoc.Load(path + @"/config.xml");
            XmlNode node = xmlDoc.SelectSingleNode("body/item");
            this.textBox1.Text = node.Attributes[0].InnerText.ToString();
            //((System.Xml.XmlAttribute)((new System.Collections.ArrayList.ArrayListDebugView(((System.Xml.XmlNamedNodeMap)(((System.Xml.XmlElement)(node)).Attributes.System.Collections.ICollection.SyncRoot)).nodes)).Items[0])).Value;// node.Value.ToString();
        }
==================================================

//更新节点


        private void button4_Click(object sender, EventArgs e)
        {
            //更新节点
            XmlDocument xmlDoc = new XmlDocument();
            string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
            xmlDoc.Load(path + @"/config.xml");
            XmlNodeList list = xmlDoc.SelectSingleNode("body").ChildNodes;
            foreach (XmlNode node in list)
            {
                XmlElement ea = (XmlElement)node;
                if (ea.GetAttribute("IMEI") != "333333")
                {
                    ea.SetAttribute("IMEI","333333");
                }
            }
            //string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName);
            xmlDoc.Save(path + "//config.xml");
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值