• 利用XmlDocument操作XML文件


    利用XmlDocument可以方便的操作XML文件。

    1.操作XML文件基本方法

    (1)添加对System.Xml的引用,并使用using语句添加引用;

    (2)假设要读取的XML文件如下:

    <?xml version="1.0" encoding="utf-8"?>
    <Students>
      <Student>
        <Name>张靓靓</Name>
        <Age>20</Age>
        <Hobby>唱歌歌</Hobby>
      </Student>
      <Student>
        <Name>周杰杰</Name>
        <Age>22</Age>
        <Hobby>耍双节棍棍</Hobby>
      </Student>
    </Students>

    (3)编写程序,增加节点:

            private static void AddNode()
            {
                XmlDocument myXml = new XmlDocument();
                myXml.Load("Student.xml"); //读取指定的XML文档
                XmlNode students = myXml.SelectSingleNode("Students");//读取Students节点
                XmlElement newStudent = myXml.CreateElement("Student");

                newStudent.SetAttribute("班级", "10软件");
                XmlElement name = myXml.CreateElement("Name");
                name.InnerText = "张三";
                XmlElement age = myXml.CreateElement("Age");
                age.InnerText = "20";
                XmlElement hobby = myXml.CreateElement("Hobby");
                hobby.InnerText = "看书";
                newStudent.AppendChild(name);
                newStudent.AppendChild(age);
                newStudent.AppendChild(hobby);
                students.AppendChild(newStudent);
                myXml.Save("Student.xml");
            }

    (4)删除节点:

            private static void DelNode()
            {
                XmlDocument myXml = new XmlDocument();
                myXml.Load("Student.xml"); //读取指定的XML文档
                XmlNode student= myXml.SelectSingleNode("//Student[Name='张德友']");//读取节点
                if(student!=null)
                    student.ParentNode.RemoveChild(student);
                myXml.Save("Student.xml");
            }

    (5)修改节点的值:

            private static void ChangeNode()
            {
                XmlDocument myXml = new XmlDocument();
                myXml.Load("Student.xml"); //读取指定的XML文档
                XmlNodeList students = myXml.DocumentElement.ChildNodes;
                foreach (XmlNode student in students)
                {
                    XmlElement name=student.SelectSingleNode("Name") as XmlElement;//读取节点
                    if (name.InnerText.Equals("张三"))
                        name.InnerText = "李四";
                }
                myXml.Save("Student.xml");
            }

    (6)读取XML文件:

            private static void ReadNode()
            {
                XmlDocument myXml = new XmlDocument();
                myXml.Load("Student.xml"); //读取指定的XML文档
                XmlNodeList students = myXml.DocumentElement.ChildNodes;//读取所有Student节点
                foreach (XmlNode student in students)// 对子节点进行循环
                {
                    foreach (XmlNode node in student.ChildNodes)
                    {
                        //将每个节点的内容显示出来
                        switch (node.Name)
                        {
                            case "Name":
                                Console.WriteLine("姓名:{0}", node.InnerText);
                                break;
                            case "Age":
                                Console.WriteLine("年龄:{0}", node.InnerText);
                                break;
                            case "Hobby":
                                Console.WriteLine("爱好:{0}", node.InnerText);
                                break;
                        }
                    }
                }
            }

    2  操作ASP.NET网站站点地图文件:

    站点地图文件Web.sitemap的格式如下:

    <?xml version="1.0" encoding="utf-8"?>
    <siteMapNode url="default.aspx" title="新书推荐" description="">
      <siteMapNode title=" C#" url="BookList.aspx?typeid=1" description="" />
      <siteMapNode title=".NET" url="BookList.aspx?typeid=25" description="" />
    </siteMapNode>

        /// <summary>
        /// 更新全部XML
        /// </summary>
        private void ModifyXml()
        {       
            xmldocTree.Load(Server.MapPath(CATAGORYXML));
            xmldocTree.SelectSingleNode("siteMapNode").InnerText="";
            foreach (Category catagory in CategoryManager.GetAllCategories())
            {
                MakeChild(catagory);
            }
            xmldocTree.Save(Server.MapPath(CATAGORYXML));
        }
        /// <summary>
        /// 设置子节点
        /// </summary>
        /// <param name="catagory"></param>
        private void MakeChild(Category catagory)
        {
            XmlElement xmlelNode = xmldocTree.CreateElement("siteMapNode");
            xmlelNode.SetAttribute("title", catagory.Name);
            xmlelNode.SetAttribute("url", BOOKLISTURL + catagory.Id.ToString());
            xmlelNode.SetAttribute("description", "");
            xmldocTree.SelectSingleNode("siteMapNode").AppendChild(xmlelNode);
        }

  • 相关阅读:
    Css3炫酷总结使用
    HTML5的你应该记住的一些知识点
    JavaScript 笔记
    Sliverlight linq中的数组筛选数据库中的数据
    理解依赖注入 for Zend framework 2
    Zend Framework 2中如何使用Service Manager
    让注册体验来得更简单点吧
    Flash中国地图 开放源码
    cocos2dx之WebView踩过的坑(android返回键处理问题)
    android游戏的增量更新(资源及代码的热更新)
  • 原文地址:https://www.cnblogs.com/zhouhb/p/2004417.html
Copyright © 2020-2023  润新知