XmlTextWriter类允许你将XML写到一个文件中去。这个类包含了很多方法和属性,使用这些属性和方法可以使你更容易地处理XML。为了使用这个类,你必须首先创建一个新的XmlTextWriter对象,然后你可以将XML片断加入到这个对象中。这个类中包含了不少的方法用于将各种类型的XML元素添加到XML文件中,下表给出了这些方法的名字和描述情况:

方法

描述

WriteStartDocument

书写版本为“1.0” XML 声明

WriteEndDocument

关闭任何打开的元素或属性

Close

关闭流

WriteDocType

写出具有指定名称和可选属性的 DOCTYPE 声明

WriteStartElement

写出指定的开始标记

WriteEndElement

关闭一个元素

WriteFullEndElement

关闭一个元素,并且总是写入完整的结束标记

WriteElementString

写出包含字符串值的元素

WriteStartAttribute

书写属性的起始内容

WriteEndAttribute

关闭上一个 WriteStartAttribute 调用

WriteRaw

手动书写原始标记 (可用于批量写入节点)

WriteString

书写一个字符串

WriteAttributeString

出具有指定值的属性

WriteCData

写出包含指定文本的 <![CDATA[…]]>

WriteComment

写出包含指定文本的注释 <!–…–>

WriteWhiteSpace

写出给定的空白

WriteProcessingInstruction

写出在名称和文本之间带有空格的处理指令,如下所示:<?name text?>    

 

using System;
using System.IO;
using System.Xml;
public class Sample
{
  public static void Main()
  {
     XmlTextWriter writer = new XmlTextWriter("titles.xml", null);
     //写入根元素
     writer.WriteStartElement("items");
     //加入子元素
     writer.WriteElementString("title", "Unreal Tournament 2003");
     writer.WriteElementString("title", "C&C: Renegade");
     writer.WriteElementString("title", "Dr. Seuss's ABC");
     //关闭根元素,并书写结束标签
     writer.WriteEndElement();
     //将XML写入文件并且关闭XmlTextWriter
     writer.Close(); 
  }
}
------------------------------------------------------------------------------------------------------------------------------------
运行结果:
<items>
    <title>Unreal Tournament 2003</title>
    <title>C&C: Renegade</title>
    <title>Dr. Seuss's ABC</title>
</items>

说明:先创建一个文档,添加一些元素,然后关闭这个文档。添加了元素后你还可以添加子元素,属性和其他内容。下面的代码就是这样的一个例子,它创建了一个文件名为title的XML文件。
using System;
using System.IO;
using System.Xml;
public class Sample
{
  public static void Main()
  {
     XmlTextWriter writer = new XmlTextWriter("myMedia.xml", null);
     //使用自动缩进便于阅读
     writer.Formatting = Formatting.Indented;
     //书写根元素
     writer.WriteStartElement("items");
     //开始一个元素
     writer.WriteStartElement("item");
     //向先前创建的元素中添加一个属性
     writer.WriteAttributeString("rating", "R");
     //添加子元素
     writer.WriteElementString("title", "The Matrix");
     writer.WriteElementString("format", "DVD");
     //关闭item元素
     writer.WriteEndElement();  // 关闭元素
     //在节点间添加一些空格
     writer.WriteWhitespace("\n");
     //使用原始字符串书写第二个元素
     writer.WriteRaw("<item>" +
                     "<title>BloodWake</title>" +
                     "<format>XBox</format>" +
                     "</item>");
     //使用格式化的字符串书写第三个元素
     writer.WriteRaw("\n  <item>\n" +
                     "    <title>Unreal Tournament 2003</title>\n" +
                     "    <format>CD</format>\n" +
                     "  </item>\n");
     // 关闭根元素
     writer.WriteFullEndElement();
     //将XML写入文件并关闭writer
     writer.Close();
  }
}
------------------------------------------------------------------------------------------------------------------------------------
运行结果:
 <items>
  <item rating="R">
    <title>The Matrix</title>
    <format>DVD</format>
  </item>
<item>
    <title>BloodWake</title>
    <format>XBox</format>
</item>
  <item>
    <title>Unreal Tournament 2003</title>
    <format>CD</format>
  </item>
</items>
说明:上面的代码创建了一个名为writer的XmlTextWriter对象。当这个对象被创建时,它被关联到一个名为titles.xml的文件。
接着,程序创建了一个叫做items的根属性,WriteStartElement方法创建了这个属性的开始标签。
接下来,程序调用了WriteElementString方法创建了三个子元素。
从上面的代码你还可以看到,这个方法使用第一个参数(在上面的程序中是title)作为元素的标签;
使用第二个参数作为元素的值。当你添加了所有的元素后,你需要关闭根元素。
这时你可以调用WriteEndElement方法关闭那个最近被打开的元素;在本例中,这个最近被打开的元素就是根元素。
当所有的数据都已经写好,根元素也已经关闭时,你可以将信息传送给你的XmlTextWriter。这意味着这时候你可以调用Close方法关闭它了。
   XmlDocument xmlDoc=new XmlDocument();
   xmlDoc.Load("bookstore.xml");
   XmlNode root=xmlDoc.SelectSingleNode("bookstore");//查找<bookstore>
   XmlElement xe1=xmlDoc.CreateElement("book");//创建一个<book>节点
   xe1.SetAttribute("genre","李赞红");//设置该节点genre属性
   xe1.SetAttribute("ISBN","2-3631-4");//设置该节点ISBN属性

   XmlElement xesub1=xmlDoc.CreateElement("title");
   xesub1.InnerText="CS从入门到精通";//设置文本节点
   xe1.AppendChild(xesub1);//添加到<book>节点中
   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);//添加到<bookstore>节点中
   xmlDoc.Save("bookstore.xml");
------------------------------------------------------------------------------------------------------------------
运行结果:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
  <book genre="fantasy" ISBN="2-3631-4">
    <title>Oberon's Legacy</title>
    <author>Corets, Eva</author>
    <price>5.95</price>
  </book>
  <book genre="李赞红" ISBN="2-3631-4">
    <title>CS从入门到精通</title>
    <author>候捷</author>
    <price>58.3</price>
  </book>
</bookstore>

说明:
已知有一个XML文件(bookstore.xml)如下,往<bookstore>节点中插入一个<book>节点:

<?xml version="1.0" encoding="gb2312"?>
<bookstore>
  <book genre="fantasy" ISBN="2-3631-4">
    <title>Oberon's Legacy</title>
    <author>Corets, Eva</author>
    <price>5.95</price>
  </book>
</bookstore>

 

   XmlNodeList nodeList=xmlDoc.SelectSingleNode("bookstore").ChildNodes;//获取bookstore节点的所有子节点
   foreach(XmlNode xn in nodeList)//遍历所有子节点
   {
    XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
    if(xe.GetAttribute("genre")=="李赞红")//如果genre属性值为“李赞红”
    {
     xe.SetAttribute("genre","update李赞红");//则修改该属性为“update李赞红”

     XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
     foreach(XmlNode xn1 in nls)//遍历
     {
      XmlElement xe2=(XmlElement)xn1;//转换类型
      if(xe2.Name=="author")//如果找到
      {
       xe2.InnerText="亚胜";//则修改
       break;//找到退出来就可以了
      }
     }
     break;
    }
   }

   xmlDoc.Save("bookstore.xml");//保存。

---------------------------------------------------------------------------------------------------- 运行结果: <?xml version="1.0" encoding="gb2312"?> <bookstore> <book genre="fantasy" ISBN="2-3631-4"> <title>Oberon's Legacy</title> <author>Corets, Eva</author> <price>5.95</price> </book> <book genre="update李赞红" ISBN="2-3631-4"> <title>CS从入门到精通</title> <author>亚胜</author> <price>58.3</price> </book> </bookstore> 说明: 接上一实例,修改节点:将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点<author>的文本修改为“亚胜”。
XmlNodeList xnl=xmlDoc.SelectSingleNode("bookstore").ChildNodes;

   foreach(XmlNode xn in xnl)
   {
    XmlElement xe=(XmlElement)xn;
    if(xe.GetAttribute("genre")=="fantasy")
    {
     xe.RemoveAttribute("genre");//删除genre属性
    }
    else if(xe.GetAttribute("genre")=="update李赞红")
    {
     xe.RemoveAll();//删除该节点的全部内容
    }
   }
   xmlDoc.Save("bookstore.xml");
------------------------------------------------------------------------------------------------------------
运行结果:
<?xml version="1.0" encoding="gb2312"?>
<bookstore>
  <book ISBN="2-3631-4">
    <title>Oberon's Legacy</title>
    <author>Corets, Eva</author>
    <price>5.95</price>
  </book>
  <book>
  </book>
</bookstore>
说明:
接上一实例、删除 <book genre="fantasy" ISBN="2-3631-4">节点的genre属性,删除 <book genre="update李赞红" ISBN="2-3631-4">节点。
 XmlNode xn=xmlDoc.SelectSingleNode("bookstore");

   XmlNodeList xnl=xn.ChildNodes;

   foreach(XmlNode xnf in xnl)
   {
    XmlElement xe=(XmlElement)xnf;
    Console.WriteLine(xe.GetAttribute("genre"));//显示属性值
    Console.WriteLine(xe.GetAttribute("ISBN"));

    XmlNodeList xnf1=xe.ChildNodes;
    foreach(XmlNode xn2 in xnf1)
    {
     Console.WriteLine(xn2.InnerText);//显示子节点点文本
    }
   }
-----------------------------------------------------------------------------------------------------------------------
说明:
显示所有数据。

 

原文地址:http://www.cnblogs.com/ViolinHuang/p/16905695.html

1. 本站所有资源来源于用户上传和网络,如有侵权请邮件联系站长! 2. 分享目的仅供大家学习和交流,请务用于商业用途! 3. 如果你也有好源码或者教程,可以到用户中心发布,分享有积分奖励和额外收入! 4. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解! 5. 如有链接无法下载、失效或广告,请联系管理员处理! 6. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需! 7. 如遇到加密压缩包,默认解压密码为"gltf",如遇到无法解压的请联系管理员! 8. 因为资源和程序源码均为可复制品,所以不支持任何理由的退款兑现,请斟酌后支付下载 声明:如果标题没有注明"已测试"或者"测试可用"等字样的资源源码均未经过站长测试.特别注意没有标注的源码不保证任何可用性