码迷,mamicode.com
首页 > 其他好文 > 详细

XML基本概念及增删改查操作

时间:2016-03-11 14:13:00      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:

一、概念及特征:

1. XML 指可扩展标记语言(Extensible Markup Language),用户可以自己定义标签。XML 被设计用来传输和存储数据,而 HTML 用于格式化并显示数据,并且HTML不能自定义标签。

2. XML 文档形成一种树结构, XML 文档必须包含根元素。该元素是所有其他元素的父元素。XML 文档中的元素形成了一棵文档树。这棵树从根部开始,并扩展到树的最底端。

3. XML中所有元素都必须有关闭标签, XML 必须正确地嵌套, XML 的属性值须加引号, XML 标签对大小写敏感。

二、基本格式示例:

<bookstore>

 <book category="CHILDREN">

<title>Harry Potter</title>

<author>J K. Rowling</author>

 <year>2005</year>

 <price>29.99</price>

 </book>

 </bookstore>

三、使用XMLDocument读写XML

protected void btnCreate_Click(object sender, EventArgs e)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(Server.MapPath("bookstore.xml"));
            XmlNode root = xmlDoc.SelectSingleNode("bookstore");
            XmlElement xe1 = xmlDoc.CreateElement("book");
            xe1.SetAttribute("genre", "李赞红");
            xe1.SetAttribute("ISBN", "2-3631-4");
            XmlElement xesub1 = xmlDoc.CreateElement("title");
            xesub1.InnerText = "CS从入门到精通";
            xe1.AppendChild(xesub1);
            XmlElement xesub2 = xmlDoc.CreateElement("author");
            xesub2.InnerText = "候捷";
            xe1.AppendChild(xesub2);
            XmlElement xesub3 = xmlDoc.CreateElement("price");
            xesub3.InnerText = "58.3";
            xe1.AppendChild(xesub3);
            root.AppendChild(xe1);
            xmlDoc.Save(Server.MapPath("bookstore.xml"));
        } 

protected void EditNodes_Click(object sender, EventArgs e)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(Server.MapPath("bookstore.xml"));
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("bookstore").ChildNodes;
            foreach (XmlNode xn in nodeList)
            {
                XmlElement xe = (XmlElement)xn;
                if (xe.GetAttribute("genre") == "李赞红")
                {
                    xe.SetAttribute("genre", "update李赞红");
                    XmlNodeList nls = xe.ChildNodes;
                    foreach (XmlNode xn1 in nls)
                    {
                        XmlElement xe2 = (XmlElement)xn1;
                        if (xe2.Name == "author")
                        {
                            xe2.InnerText = "亚胜";
                            break;
                        }
                    }
                    break;
                }
            }
            xmlDoc.Save(Server.MapPath("bookstore.xml"));
        }

 protected void btnDelete_Click(object sender, EventArgs e)
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(Server.MapPath("bookstore.xml"));
            XmlNodeList xnl = xmlDoc.SelectSingleNode("bookstore").ChildNodes;
            foreach (XmlNode xn in xnl)
            {
                XmlElement xe = (XmlElement)xn;
                if (xe.GetAttribute("genre") == "fantasy")
                {
                    xe.RemoveAttribute("genre");
                }
                else if (xe.GetAttribute("genre") == "update李赞红")
                {
                    xe.RemoveAll();
                }
            }
            xmlDoc.Save(Server.MapPath("bookstore.xml"));
        } 

四、使用LINQ to XML读写XML,LINQ to XML 最重要的优势是它与 Language-Integrated Query (LINQ) 的集成。

 private void CreateXmlFile()
        {   ///设置新的XML文件保存的地址
            string xmlFilePath = Server.MapPath("Books.xml");
            XDocument doc = new XDocument(
                     new XDeclaration("1.0", "utf-8", "no"),
                     new XElement("Books",
                              new XElement("Book",
                                        new XAttribute("ID", "104"),                       ///添加属性ID
                                        new XElement("No", "0004"),                     ///添加元素No
                                        new XElement("Name", "Book 0004"),             ///添加元素Name
                                        new XElement("Price", "300"),                    ///添加元素Price
                                        new XElement("Remark", "This is a book 0004.")    ///添加元素Remark
                                        )
                              )
                     );
            ///保存为XML文件
            doc.Save(xmlFilePath);
            ///显示XML文件的内容
            Response.Write(doc);
            ///设置网页显示的形式为XML文件
            Response.ContentType = "text/xml";
            Response.End();
        }

private void AddXmlElement()
        {        ///导入XML文件
            string xmlFilePath = Server.MapPath("Books.xml");
            XElement xe = XElement.Load(xmlFilePath);
            ///创建一个新的节点
            XElement book = new XElement("Book",
                     new XAttribute("ID", "105"),                       ///添加属性ID
                     new XElement("No", "0005"),                     ///添加元素No
                     new XElement("Name", "Book 0005"),             ///添加元素Name
                     new XElement("Price", "500"),                    ///添加元素Price
                     new XElement("Remark", "This is a book 0005.")   ///添加元素Remark
                     );
            ///添加节点到文件中,并保存
            xe.Add(book);
            xe.Save(xmlFilePath);
            ///显示XML文件的内容
            Response.Write(xe);
            ///设置网页显示的形式为XML文件
            Response.ContentType = "text/xml";
            Response.End();
        }

private void UpdateXmlElement()
        {///导入XML文件
            string xmlFilePath = Server.MapPath("Books.xml");
            XElement xe = XElement.Load(xmlFilePath);
            ///查找被替换的元素
            IEnumerable<XElement> element = from e in xe.Elements("Book")
                                            where e.Attribute("ID").Value == "104"
                                            select e;
            ///替换为新元素,并保存
            if (element.Count() > 0)
            {
                XElement first = element.First();
                ///设置新的属性
                first.SetAttributeValue("ID", "106");
                ///替换新的节点
                first.ReplaceNodes(
                         new XElement("No", "0006"),                      ///添加元素No
                         new XElement("Name", "Book 0006"),              ///添加元素Name
                         new XElement("Price", "600"),                     ///添加元素Price
                         new XElement("Remark", "This is a book 0006.")     ///添加元素Remark
                         );
            }
            xe.Save(xmlFilePath);
            ///显示XML文件的内容
            Response.Write(xe);
            ///设置网页显示的形式为XML文件
            Response.ContentType = "text/xml";
            Response.End();
        }

private void RemoveXmlElement()
        {
            ///导入XML文件
            string xmlFilePath = Server.MapPath("Books.xml");
            XElement xe = XElement.Load(xmlFilePath);
            ///查找被删除的元素
            IEnumerable<XElement> element = from e in xe.Elements()
                                            where (string)e.Element("Name") == "Book 0006"
                                            select e;
            ///删除指定的元素,并保存
            if (element.Count() > 0) { element.First().Remove(); }
            xe.Save(xmlFilePath);
            ///显示XML文件的内容
            Response.Write(xe);
            ///设置网页显示的形式为XML文件
            Response.ContentType = "text/xml";
            Response.End();
        }

XML基本概念及增删改查操作

标签:

原文地址:http://www.cnblogs.com/xiaxianfei/p/5265242.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!