XmlTextReader和XmlTextWriter是.net提供的两个快速操作xml文件的类。
XmlTextReader
XmlTextReader提供对 XML 数据进行快速、非缓存、只进访问的读取器。 XmlTextReader首先打开文件,然后从文件中读取信息,最后关闭文件。每次从文件中读取信息时,它在文件中的位置都会前移,到达文件结尾的时候就不读了,类似于读取普通的文本文件。
读取代码:
protected void Button1_Click(object sender, EventArgs e) ??????? { ??????????? StringBuilder objStringBuilder = new StringBuilder(); ??????????? objStringBuilder.Append("<br/>"); ??????????? //初始化,并指定要读取的文档 ??????????? XmlTextReader objXmlTextReader = new XmlTextReader(Server.MapPath("Softs.xml")); ??????????? //只进读取,直到文档结尾 ??????????? while (objXmlTextReader.Read()) ??????????? { ??????????????? //判断节点类型,默认会把空格和换行符读取为节点 ??????????????? if (objXmlTextReader.NodeType == XmlNodeType.Element) ??????????????? { ??????????????????? //节点名称 ??????????????????? if (objXmlTextReader.Name == "Soft") ??????????????????? { ??????????????????????? //先读取属性,因为读取子节点后,会读取到EndElement ??????????????????????? string addDate = objXmlTextReader.GetAttribute("AddDate"); ??????????????????????? //读取子节点 ??????????????????????? XmlReader objXmlReader = objXmlTextReader.ReadSubtree(); ??????????????????????? while (objXmlReader.Read()) ??????????????????????? { ??????????????????????????? if (objXmlReader.Name == "Name") ??????????????????????????? { ??????????????????????????????? objStringBuilder.Append("软件名称:"); ??????????????????????????????? objStringBuilder.Append(objXmlReader.ReadElementContentAsString()); ??????????????????????????????? objStringBuilder.Append("<br/>"); ??????????????????????????? } ??????????????????????????? if (objXmlReader.Name == "Author") ??????????????????????????? { ??????????????????????????????? objStringBuilder.Append("软件作者:"); ??????????????????????????????? objStringBuilder.Append(objXmlReader.ReadElementContentAsString()); ??????????????????????????????? objStringBuilder.Append("<br/>"); ??????????????????????????? } ??????????????????????????? if (objXmlReader.Name == "Description") ??????????????????????????? { ??????????????????????????????? objStringBuilder.Append("软件简介:"); ??????????????????????????????? objStringBuilder.Append(objXmlReader.ReadElementContentAsString()); ??????????????????????????????? objStringBuilder.Append("<br/>"); ??????????????????????????? } ??????????????????????? } ??????????????????????? objStringBuilder.Append("添加日期:"); ??????????????????????? objStringBuilder.Append(addDate); ??????????????????????? objStringBuilder.Append("<br/>"); ??????????????????? } ??????????????? } ??????????? } ??????????? Literal2.Text = objStringBuilder.ToString(); ??????? }
?
XmlTextWriter
XmlTextWriter 提供快速、非缓存、只进方法的编写器,该方法生成包含XML 数据(这些数据符合W3C 可扩展标记语言(XML) 1.0 和“XML 中的命名空间”建议)的流或文件。
创建代码:
?protected void Button1_Click(object sender, EventArgs e) ??????? { ??????????? //初始化对象 ??????????? XmlTextWriter objXmlTextWriter = new XmlTextWriter(Server.MapPath("Softs2.xml"), System.Text.Encoding.UTF8); ??????????? //元素缩进 ??????????? objXmlTextWriter.Formatting = Formatting.Indented; ??????????? //书写根元素 ??????????? objXmlTextWriter.WriteStartElement("Softs"); ??????????? //开始一个元素 ??????????? objXmlTextWriter.WriteStartElement("Soft"); ??????????? //向先前创建的元素中添加一个属性 ??????????? objXmlTextWriter.WriteAttributeString("AddDate", DateTime.Now.ToString()); ??????????? //添加子元素 ??????????? objXmlTextWriter.WriteElementString("Name", "DHCMS"); ??????????? objXmlTextWriter.WriteElementString("Author", "BOSSMA"); ??????????? //关闭元素 ??????????? objXmlTextWriter.WriteEndElement();? // 关闭元素 ??????????? //使用格式化的字符串书写第2个元素 ??????????? objXmlTextWriter.WriteRaw("\n? <Soft AddDate=\"" + DateTime.Now.ToString() + "\">\n" + ??????????????????????????? "??? <Name>ECSHOP</Name>\n" + ??????????????????????????? "??? <Author>Discuz</Author>\n" + ??????????????????????????? "? </Soft>\n"); ??????????? // 关闭根元素 ??????????? objXmlTextWriter.WriteFullEndElement(); ??????????? //将XML写入文件并关闭writer ??????????? objXmlTextWriter.Close(); ??????? } ??? }
发表评论
相关文章
国内AI资源汇总,AI聊天、AI绘画、AI写作、AI视频、AI设计、AI编程、AI音乐等,国内顺畅访问,无需科学上网。
扫码或点击进入:萤火AI大全
文章分类
最新评论