编写XML XmlTextWriter与XmlDocument(转载)
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(转载)相关推荐
- 编写XML作为配置文件的高级操作库
编写XML作为配置文件的高级操作库 yipsilon 原创 (参与分:293,专家分:180) 发表:2003-7-29 下午4:26 更新:2003-7-30 上午8:30 版本:1. ...
- 使用记事本编写xml文件保存出现异常原因
在涉及中文的xml文件中,首先在eclipse中声明xml文件时输入<?version="1.0"encoding="gb2312" ?>保存文件, ...
- 用php编写xml,PHP 读取和编写 XML
什么是 XML? XML 是一种数据存储格式.它没有定义保存什么数据,也没有定义数据的格式.XML 只是定义了标记和这些标记的属性.格式良好的 XML 标记看起来像这样: 代码如下: Jack Her ...
- 用 PHP 读取和编写 XML DOM
简单代码: <?php $str = file_get_contents('test.xml'); //如果不是utf8的编码 //$xml = simplexml_load_string(ic ...
- java编写大数据分析模型_如何用Java(DOM分析器)编写XML文件
java编写大数据分析模型 Earlier we learned how to read XML file and how to edit XML file in java using DOM Par ...
- 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 ...
- 【C#】创建、解析 xml 文件(XmlDocument 方式)
前言 本文使用 System.Xml 中的 XmlDocument 解析 xml 格式的文件.另外,由于我是粗略的看了下官方文档和一些博客,可能会有许多错误的地方,望指出. 官方文档:https:// ...
- C#操作Xml:通过XmlDocument读写Xml文档
http://www.cnblogs.com/yukaizhao/archive/2011/07/19/csharp_xmldocument_access_xml.html 什么是Xml? Xml是扩 ...
- BAT文件语法和技巧(bat文件的编写及使用)(转载一)
BAT文件语法和技巧(bat文件的编写及使用)(转载一) 比较有用的东比较有用的东西 首先, 批处理 文件是一个文本文件,这个文件的每一行都是一条DOS命令(大部分时候就好象我们在DOS提示符下执行 ...
最新文章
- CVPR和ICLR双榜公布,最离谱审稿人竟然没读论文!
- from .pycaffe import Net, SGDSolver, NesterovSolver, AdaGradSolver, RMSPropSolver, AdaDeltaSolver,
- Handler Bundle Runnable
- FCKeditor 在ASP.Net 中的使用说明
- Raw264.7培养经验分享
- m_Orchestrate learning system---二十、如何写代码不容易犯错
- [java] javax.el.PropertyNotFoundException: Property 'id' not found on type bean.Student
- [国家集训队] 特技飞行
- vue 组件根元素显示优化
- SpringBoot 如何防御 CSRF 攻击?
- 小朋友的经典造句,现在做老师的太不容易了
- Spring AOP--Aspect的CGLib方式
- 【Udacity笔记】What is Machine Learning?
- 等参元:平面四节点四边形等参元的刚度矩阵的计算
- linux ubantu最新版本,过去十年最佳的Ubuntu版本
- C++面经与嵌入式软件面经(蒋豆芽专栏总结)完成了!
- 解决EPSON R230打印机驱动程序安装错误
- 移动光猫连接移动硬盘变成超小型nas【HS8545M5
- netty--关于NIO和OIO
- imx6ul双网口(LAN8720A)