XmlTextWriter类可以把XML写入一个流、文件或TextWriter对象中。

简单例子:
  private void button2_Click(object sender, System.EventArgs e)
  {
   string filename = "booknew.xml";
   XmlTextWriter tw = new XmlTextWriter(filename,null);
   tw.Formatting = Formatting.Indented;
   tw.WriteStartDocument();
   
   tw.WriteStartElement("book");
   tw.WriteAttributeString("genre","Mystery");
   tw.WriteAttributeString("publicationdate","2001");
   tw.WriteAttributeString("ISBN","123456789");
   tw.WriteElementString("title","Case of the Missing Cookie");
   tw.WriteStartElement("author");
   tw.WriteElementString("name","Cookie Monster");
   tw.WriteEndElement();
   tw.WriteElementString("price","9.99");
   tw.WriteEndElement();
   tw.WriteEndDocument();
   tw.Flush();
   tw.Close();
  }

代码生成后的xml文档booksnew.xml:

<?xml version="1.0"?>
<book genre="Mystery" publicationdate="2001" ISBN="123456789">
  <title>Case of the Missing Cookie</title>
  <author>
    <name>Cookie Monster</name>
  </author>
  <price>9.99</price>
</book>

可 以看出,在XML文档中,有一个起始方法和结束方法(WriteStartElement和WriteEndElement),其他专用的写入方 法:WriteCData可以输入一个Cdata;WriteComment以正确的XML格式写入注释。WriteChars写入字符缓冲区的内容。

利用.NET DOM,XmlDocument创建一个文档

private XmlDocument doc= new XmlDocument();
  private void button2_Click(object sender, System.EventArgs e)
  {
     XmlDeclaration newDec = doc.CreateXmlDeclaration("1.0",null,null);
     doc.AppendChild(newDec);
     XmlElement newRoot = doc.CreateElement("newBookstore");
     doc.AppendChild(newRoot);

//创建一个新的book元素
     XmlElement newBook = doc.CreateElement("book");
     //创建并设置book元素的属性
     newBook.SetAttribute("genre","Mystery");
     newBook.SetAttribute("publicationdate","2001");
     newBook.SetAttribute("ISBN","123456789");
     //创建一个title元素
     XmlElement newTilte = doc.CreateElement("title");
     newTilte.InnerText  ="Case of the Missing Cookie";
     newBook.AppendChild(newTilte);
     //创建author元素
     XmlElement newAuthor = doc.CreateElement("author");
     newBook.AppendChild(newAuthor);

XmlElement newName = doc.CreateElement("name");
     newName.InnerText  = "C.Monster";
     newAuthor.AppendChild(newName);

XmlElement newPrice = doc.CreateElement("price");
     newPrice.InnerText = "9.95";
     newBook.AppendChild(newPrice);
     doc.DocumentElement.AppendChild(newBook);
     XmlTextWriter tr = new XmlTextWriter("booksEdit.xml",null);
     tr.Formatting = Formatting.Indented;
     doc.WriteContentTo(tr);
     tr.Close();
}

代码生成后的文档:
<?xml version="1.0"?>
<newBookstore>
  <book genre="Mystery" publicationdate="2001" ISBN="123456789">
    <title>Case of the Missing Cookie</title>
    <author>
      <name>C.Monster</name>
    </author>
    <price>9.95</price>
  </book>
</newBookstore>

如 果从头开始创建一个文档,可以使用XmlTextWrite。还可以使用XmlDocument。使用哪个比较好?如果要写入Xml流的数据已经准备好, 最好的选择用XmlTextWriter类,但是如果需要一次建立Xml文档的一小部分,在不同的地方插入节点,用XmlDocument创建文档就比较 好。

转载于:https://www.cnblogs.com/chenying99/archive/2011/03/28/1997384.html

编写XML XmlTextWriter与XmlDocument(转载)相关推荐

  1. 编写XML作为配置文件的高级操作库

    编写XML作为配置文件的高级操作库 yipsilon 原创  (参与分:293,专家分:180)   发表:2003-7-29 下午4:26   更新:2003-7-30 上午8:30   版本:1. ...

  2. 使用记事本编写xml文件保存出现异常原因

    在涉及中文的xml文件中,首先在eclipse中声明xml文件时输入<?version="1.0"encoding="gb2312" ?>保存文件, ...

  3. 用php编写xml,PHP 读取和编写 XML

    什么是 XML? XML 是一种数据存储格式.它没有定义保存什么数据,也没有定义数据的格式.XML 只是定义了标记和这些标记的属性.格式良好的 XML 标记看起来像这样: 代码如下: Jack Her ...

  4. 用 PHP 读取和编写 XML DOM

    简单代码: <?php $str = file_get_contents('test.xml'); //如果不是utf8的编码 //$xml = simplexml_load_string(ic ...

  5. java编写大数据分析模型_如何用Java(DOM分析器)编写XML文件

    java编写大数据分析模型 Earlier we learned how to read XML file and how to edit XML file in java using DOM Par ...

  6. java xml stax_如何使用Java StAX Iterator API用Java编写XML文件

    java xml stax Java Streaming API for XML or Java StAX API was introduced in Java 6 and considered su ...

  7. 【C#】创建、解析 xml 文件(XmlDocument 方式)

    前言 本文使用 System.Xml 中的 XmlDocument 解析 xml 格式的文件.另外,由于我是粗略的看了下官方文档和一些博客,可能会有许多错误的地方,望指出. 官方文档:https:// ...

  8. C#操作Xml:通过XmlDocument读写Xml文档

    http://www.cnblogs.com/yukaizhao/archive/2011/07/19/csharp_xmldocument_access_xml.html 什么是Xml? Xml是扩 ...

  9. BAT文件语法和技巧(bat文件的编写及使用)(转载一)

    BAT文件语法和技巧(bat文件的编写及使用)(转载一) 比较有用的东比较有用的东西  首先, 批处理 文件是一个文本文件,这个文件的每一行都是一条DOS命令(大部分时候就好象我们在DOS提示符下执行 ...

最新文章

  1. CVPR和ICLR双榜公布,最离谱审稿人竟然没读论文!
  2. from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver,
  3. Handler Bundle Runnable
  4. FCKeditor 在ASP.Net 中的使用说明
  5. Raw264.7培养经验分享
  6. m_Orchestrate learning system---二十、如何写代码不容易犯错
  7. [java] javax.el.PropertyNotFoundException: Property 'id' not found on type bean.Student
  8. [国家集训队] 特技飞行
  9. vue 组件根元素显示优化
  10. SpringBoot 如何防御 CSRF 攻击?
  11. 小朋友的经典造句,现在做老师的太不容易了
  12. Spring AOP--Aspect的CGLib方式
  13. 【Udacity笔记】What is Machine Learning?
  14. 等参元:平面四节点四边形等参元的刚度矩阵的计算
  15. linux ubantu最新版本,过去十年最佳的Ubuntu版本
  16. C++面经与嵌入式软件面经(蒋豆芽专栏总结)完成了!
  17. 解决EPSON R230打印机驱动程序安装错误
  18. 移动光猫连接移动硬盘变成超小型nas【HS8545M5
  19. netty--关于NIO和OIO
  20. imx6ul双网口(LAN8720A)

热门文章

  1. ACM-尼姆博弈之取(m堆)石子游戏——hdu2176
  2. 在osx 10.10.3 下安装git总结
  3. 上海交通大学2002年数学分析考研试题
  4. JavaScript:内存泄露
  5. 如何利用 Visual Studio 自带工具提高开发效率
  6. 我的596升级到Windows Mobile 6了
  7. 深度学习 循环神经网络RNN
  8. Matlab怎样将传递函数转换成差分方程
  9. 使用PIL库使用文本生成图片(类验证码)
  10. TENSORFLOW 指定使用GPU跑