XML教程

来源:互联网 发布:网络终端与电脑终端 编辑:程序博客网 时间:2024/05/17 21:53

功能说明:

      1、使用XmlTextReader读取XML文件数据

      2、使用XmlDocument读取XML文件数据

      3、使用XmlDocument新增或修改XML节点

    

   一、XML文件

     <?xml version="1.0" encoding="utf-8" ?>

<catalog>

  <book id="bk101">

    <author>Gambardella, Matthew</author>

    <title>XML Developer's Guide</title>

    <genre>Computer</genre>

    <price>44.95</price>

    <publish_date>2000-10-01</publish_date>

    <description>

      An in-depth look at creating applications

      with XML.

    </description>

  </book>

  <book id="bk102">

    <author>Ralls, Kim</author>

    <title>Midnight Rain</title>

    <genre>Fantasy</genre>

    <price>5.95</price>

    <publish_date>2000-12-16</publish_date>

    <description>

      A former architect battles corporate zombies,

      an evil sorceress, and her own childhood to become queen

      of the world.

    </description>

  </book>

</catalog>

 二、使用XmlTextReader读取XML文件数据

     1、说明:XmlTextReader表示提供对 XML 数据进行快速、非缓存、只进访问的读取器。类似于ADO.NET里的DataReader

     2、代码

  XmlTextReader xmlTextReader = new XmlTextReader(@"Books.xml");

        xmlTextReader.WhitespaceHandling = WhitespaceHandling.None;

        while (xmlTextReader.Read())  //从上到下,依次读取XML文件的每一行

        {

            if (xmlTextReader.Name == "book")  //如果是book节点

            {

                String str=xmlTextReader.GetAttribute("id"); //获取book节点的属性

                xmlTextReader.Read();  //读下一行,这里读取到author 节点

//读取author 节点内容并跳到下一行

                string author = xmlTextReader.ReadElementContentAsString();                string title = xmlTextReader.ReadElementContentAsString();

                string genre = xmlTextReader.ReadElementContentAsString();

                string price = xmlTextReader.ReadElementContentAsString();

                string publishDate = xmlTextReader.ReadElementContentAsString();

        //读取后跳到</book> 结束节点

                string description = xmlTextReader.ReadElementContentAsString();

            }

        }

        xmlTextReader.Close();  //使用完需关闭

三、使用XmlDocument读取XML文件数据

  1、说明:XmlDocument是一次性把XML数据读取到内存中进行操作,所以不用close()方法关闭

  2、代码

  XmlDocument xmlDocument = new XmlDocument();

        xmlDocument.Load(@"Books.xml");

        XmlNodeList xmlNodes = xmlDocument.GetElementsByTagName("book");

        foreach (XmlNode node in xmlNodes)

        {

            Console.WriteLine(node.Attributes["id"].Value + ":");

            XmlNodeReader xmlNodeReader = new XmlNodeReader(node);  //绑定时并不自动定位到book节点,必须调用read移动游标

            xmlNodeReader.Read();  //读到book节点

            xmlNodeReader.Read();  //读到author节点

            string author = xmlNodeReader.ReadElementContentAsString(); //读取并移动游标到下一行

            string title = xmlNodeReader.ReadElementContentAsString();

            string genre = xmlNodeReader.ReadElementContentAsString();

            string price = xmlNodeReader.ReadElementContentAsString();

            string publishDate = xmlNodeReader.ReadElementContentAsString();

            string description = xmlNodeReader.ReadElementContentAsString();

        }

四、使用XmlDocument新增或修改XML节点

            1、新增节点

XmlDocument xmlDocument = new XmlDocument();

         xmlDocument.Load(@"Books.xml");

//创建元素节点

  XmlElement newElement = xmlDocument.CreateElement("book");

        //创建属性节点

        XmlAttribute newAttribute = xmlDocument.CreateAttribute("id");

        newAttribute.Value = "bk103";

        //创建元素节点

        XmlElement authorElement = xmlDocument.CreateElement("author");

        authorElement.InnerText = "Mark Russinovich,David Solomon,Alex Ionecsu";

        XmlElement titleElement = xmlDocument.CreateElement("title");

        titleElement.InnerText = "Windows Internals, 5th edition";

        XmlElement genreElement = xmlDocument.CreateElement("genre");

        genreElement.InnerText = "Windows Server 2008";

        XmlElement priceElement = xmlDocument.CreateElement("price");

        priceElement.InnerText = "69.99";

        XmlElement publishDateElement = xmlDocument.CreateElement("publish_date");

        publishDateElement.InnerText = "2009-6-17";

        XmlElement descriptionElement = xmlDocument.CreateElement("description");

        descriptionElement.InnerText = "Windows Internals, 5th edition is the" +

            " update to Windows Internals, 4th edition to cover Windows Vista" +

            " and Windows Server 2008 (32-bit and 64-bit).";

        //添加属性节点

        newElement.Attributes.Append(newAttribute);

        //添加元素节点

        newElement.AppendChild(authorElement);

        newElement.AppendChild(titleElement);

        newElement.AppendChild(genreElement);

        newElement.AppendChild(priceElement);

        newElement.AppendChild(publishDateElement);

        newElement.AppendChild(descriptionElement);

        xmlDocument.DocumentElement.AppendChild(newElement);

        //保存XML文档

         xmlDocument.Save("Modified Books.xml");  

2、修改节点

XmlDocument xmlDocument = new XmlDocument();

          xmlDocument.Load(@"Books.xml");

           //定位到要修改的节点(SelectSingleNode查询第一个符合条件的节点)

  XmlNode nodeToModify = xmlDocument.DocumentElement.SelectSingleNode(

            "book/genre");

nodeToModify.InnerText = "XML Tech"//修改节点内容

  //保存XML文档

          xmlDocument.Save("Modified Books.xml");