C#中XmlTextWriter读写xml文件详细介绍
XmlTextWriter类允许你将XML写到一个文件中去。这个类包含了很多方法和属性,使用这些属性和方法可以使你更容易地处理XML。为了使用这个类,你必须首先创建一个新的XmlTextWriter对象,然后你可以将XML片断加入到这个对象中。这个类中包含了不少的方法用于将各种类型的XML元素添加到XML文件中,下表给出了这些方法的名字和描述情况: 方法 如果你对于XML十分熟悉,那么你一定能很好的理解上面的这些方法。下面我们将给出一个例子,在这个例子中,我们将先创建一个文档,添加一些元素,然后关闭这个文档。添加了元素后你还可以添加子元素,属性和其他内容。下面的代码就是这样的一个例子,它创建了一个文件名为title的XML文件。 复制代码 代码如下: 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(); } } 如果你编译并且执行上面的代码,你将创建这个XML文件,文件中包含如下内容: 复制代码 代码如下: <items> <title>Unreal Tournament 2003</title> <title>C&C: Renegade</title> <title>Dr. Seuss's ABC</title> </items> 上面的代码创建了一个名为writer的XmlTextWriter对象。当这个对象被创建时,它被关联到一个名为titles.xml的文件。接着,程序创建了一个叫做items的根属性,WriteStartElement方法创建了这个属性的开始标签。接下来,程序调用了WriteElementString方法创建了三个子元素。从上面的代码你还可以看到,这个方法使用第一个参数(在上面的程序中是title)作为元素的标签;使用第二个参数作为元素的值。当你添加了所有的元素后,你需要关闭根元素。这时你可以调用WriteEndElement方法关闭那个最近被打开的元素;在本例中,这个最近被打开的元素就是根元素。当所有的数据都已经写好,根元素也已经关闭时,你可以将信息传送给你的XmlTextWriter。这意味着这时候你可以调用Close方法关闭它了。 上面的代码相对十分的简单,下面我们看一个使用了XmlTextWriter类中更多方法,功能更加完善的例子。 复制代码 代码如下: 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(); } } 上面代码编译运行后将得到myMedia.xml文件,文件的内容为: 复制代码 代码如下: <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> 上面代码中的注释说明了这个程序的功能是如何实现的。需要记住的一件事是:当调用方法开始一个操作时,你需要在程序的合适的地方调用方法结束这个操作。例如,你调用了StartElement,你就必须调用EndElement关闭元素;当然在这两个调用之间你也可以加入一个子元素。无论你何时调用EndElement方法,它总是关闭最近使用StartElement方法打开的那个元素(这和栈的工作方式很相似)。 使用XmlTextWriter十分的容易,不过我还是建议你自己动手试试这些代码和方法。你试过以后会发现这些代码能够很容易地集成到你的程序中。你还应该记住,XmlTextWriter仅仅是.NET提供的众多XML类中的一个。和XmlTextWriter一样,其他的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> </bookstore> 1、往<bookstore>节点中插入一个<book>节点: 复制代码 代码如下: 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> 2、修改节点:将genre属性值为“李赞红“的节点的genre值改为“update李赞红”,将该节点的子节点<author>的文本修改为“亚胜”。 复制代码 代码如下: 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> 3、删除 <book genre="fantasy" ISBN="2-3631-4">节点的genre属性,删除 <book genre="update李赞红" ISBN="2-3631-4">节点。 复制代码 代码如下: 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> 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);//显示子节点点文本 } } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |