欢迎来到天天文库
浏览记录
ID:34722046
大小:49.15 KB
页数:4页
时间:2019-03-10
《c#程序:如何创建xml文件以及xml文件的增、删、改、查》由会员上传分享,免费在线阅读,更多相关内容在工程资料-天天文库。
1、C#程序:如何创建xml文件以及xml文件的增、删、改、查 其实今天的这篇博文,是对请几天发表的博文的一个总结,只是想把xml文件的增删改查结合起来,这样更容易让初学的朋友理解,废话也不多说了,开始吧!下面是我把我在vs环境下写的代码ctrl+V然后ctrl+V了的,课根据自己的需要执行相应的方法 usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Xml;usingSystem.I
2、O;namespaceWriteXml{ classProgram { staticvoidMain(string[]args) { //createXml();//创建文件和添加基本数据,便于之后操作实验 //addItem();//追加数据 readtext();//读取数据 //updatexml();//更新数据 //delnode();//删除数据 }
3、privatestaticvoidcreateXml() { XmlTextWriterwriter=newXmlTextWriter("titles.xml",null); //使用自动缩进便于阅读 writer.Formatting=Formatting.Indented; //写入根元素 writer.WriteStartElement("items"); writer.Write
4、StartElement("item"); //写入属性及属性的名字 writer.WriteAttributeString("类别","文学"); writer.WriteAttributeString("品质","优"); //加入子元素 writer.WriteElementString("title","毛著"); writer.WriteElementString("author","毛泽
5、东"); writer.WriteElementString("price","10.0"); //关闭根元素,并书写结束标签 writer.WriteEndElement(); writer.WriteEndElement(); //将XML写入文件并且关闭XmlTextWriter writer.Close(); } privatestaticvoidaddItem()
6、 { XmlDocumentxmlDoc=newXmlDocument(); xmlDoc.Load("titles.xml"); XmlNoderoot=xmlDoc.SelectSingleNode("items"); XmlElementxe1=xmlDoc.createElement_x("item"); xe1.SetAttribute("类别","张三"); xe1.SetAt
7、tribute("品质","老师"); XmlElementxmlsub1=xmlDoc.createElement_x("title"); xmlsub1.InnerText="wahahha"; xe1.AppendChild(xmlsub1); XmlElementxmlsub2=xmlDoc.createElement_x("author"); xmlsub2.InnerText="三毛";
8、 xe1.AppendChild(xmlsub2); XmlElementxmlsub3=xmlDoc.createElement_x("Price"); xmlsub3.InnerText="15.00"; xe1.AppendChild(xmlsub3); ro
此文档下载收益归作者所有