四、 编写XML文档
.NET System.XML命名空间下提供的一些类库使得开发人员可以很方便的对XML进行读、写、查。
XmlTextWriter类允许你将XML写到一个文件中去。这个类包含了很多方法和属性,使用这些属性和方法可以使你更容易地处理XML。下表是XmlTextWriter的方法:
方法名 |
|
WriteStartDocument |
版本为“1.0”的 XML 声明 |
WriteEndDocument |
|
Close |
关闭此流和基础流 |
WriteDocType |
|
WriteStartElement |
|
WriteEndElement |
|
WriteFullEndElement |
|
WriteElementString |
写出包含字符串值的元素 |
WriteStartAttribute |
|
WriteEndAttribute |
|
Writeraw |
手动书写原始标记 |
WriteString |
书写一个字符串 |
WriteAttributeString |
出具有指定值的属性 |
WriteCData |
写出包含指定文本的 <![CDATA[...]]> 块 |
WriteComment |
写出包含指定文本的注释 <!--...--> |
WriteWhiteSpace |
写出给定的空白 |
WriteProcessingInstruction |
写出在名称和文本之间带有空格的处理指令,如下所示:<?name text?> |
下例是用XmlTextWriter类写的一个简单XML文档:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
namespace XML
{
class Program
{
static void Main(string[] args)
{
XmlTextWriter writer = new XmlTextWriter("demo.xml",Encoding.UTF8);
//设置自动换行缩进
writer.Formatting = Formatting.Indented;
//XML版本声明
writer.WriteStartDocument();
//根元素
writer.WriteStartElement("Student");
//扩展子元素,这里只写了2个
writer.WriteElementString("Name","长江");
writer.WriteElementString("Name","黄河");
writer.WriteEndElement();
//将XML写入文件并且关闭XmlTextWriter实例对象
writer.Close();
}
}
}
运行后,会在项目根目录/bin/debug文件夹下生成demo.xml,代码如下:
<?xml version="1.0" encoding="utf-8"?>
<Student>
<Name>长江</Name>
<Name>黄河</Name>
</Student>
下面例子,我们增加一些XmlTextWriter类中的方法的应用:
XmlTextWriter writer = new XmlTextWriter("book.xml",null);
writer.WriteStartDocument();
//设置自动缩进
writer.Formatting = Formatting.Indented;
//根元素
writer.WriteStartElement("Books");
//第一个元素
writer.WriteStartElement("Book");
writer.WriteAttributeString("BookName",".NET框架");
//添加子元素
writer.WriteElementString("Author","Jim");
writer.WriteElementString("Price","40.00");
//关闭item元素
writer.WriteEndElement(); // 关闭元素
//在节点间添加一些空格
writer.WriteWhitespace("\n");
//使用原始字符串书写第二个元素
writer.Writeraw("<Book BookName=\"C#\">" +
"<Author>Tom</Author>" +
"<Price>60.00</Price>" +
"</Book>");
//使用格式化的字符串书写第三个元素
writer.Writeraw("\n <Book BookName=\"ASP.NET\">\n" +
" <Author>David</Author>\n" +
" <Price>100.00</Price>\n" +
" </Book>\n");
// 关闭根元素
writer.WriteFullEndElement();
writer.Close();
<?xml version="1.0"?>
<Books>
<Book BookName=".NET框架">
<Author>Jim</Author>
<Price>40.00</Price>
</Book>
<Book BookName="C#"><Author>Tom</Author><Price>60.00</Price></Book>
<Book BookName="ASP.NET">
<Author>David</Author>
<Price>100.00</Price>
</Book>
</Books>
五、 XML读、查、删
上面我们学习了用XmlTextWriter对象创建一个XML文档,在本节里,将继续介绍如何在已有XML文档中查询和插入节点。下面示例在book.xml根节点下加入新的<Book>节点:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("book.xml");
//查找<Books>
XmlNode root = xmlDoc.SelectSingleNode("Books");
//创建一个<Book>节点
XmlElement el = xmlDoc.CreateElement("Book");
//设置<Book>节点的属性BookName
el.SetAttribute("BookName","Windows Application");
//创建<Book>节点的第一个下级节点
XmlElement elSubAuthor = xmlDoc.CreateElement("Author");
elSubAuthor.InnerText = "Jack";
el.AppendChild(elSubAuthor);
//创建<Book>节点的第二个下级节点
XmlElement elSubPrice = xmlDoc.CreateElement("Price");
elSubPrice.InnerText = "70.00";
el.AppendChild(elSubPrice);
//添加<Book>节点到<Books>中
root.AppendChild(el);
xmlDoc.Save("book.xml");
运行代码后,book.xml代码如下,加粗部分是新增的代码:
<?xml version="1.0"?>
<Books>
<Book BookName=".NET框架">
<Author>Jim</Author>
<Price>40.00</Price>
</Book>
<Book BookName="C#">
<Author>Tom</Author>
<Price>60.00</Price>
</Book>
<Book BookName="ASP.NET">
<Author>David</Author>
<Price>100.00</Price>
</Book>
<Book BookName="Windows Application">
<Author>Jack</Author>
<Price>70.00</Price>
</Book>
</Books>
下面示例演示了如何修改XML节点:
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("book.xml");
XmlNodeList nodeList = xmlDoc.SelectSingleNode("Books").ChildNodes;
foreach (var node in nodeList)
{
XmlElement el = (XmlElement)node;
if (el.GetAttribute("BookName") == "C#")
{
el.SetAttribute("BookName","精通C#");
}
}
xmlDoc.Save("book.xml");
如果要输出book.xml文档中所有节点的属性和值,可以尝试在上例foreach中实现。
运行后,book.xml的代码如下,原来的“C#”被更改为了“精通C#”。
<?xml version="1.0"?>
<Books>
<Book BookName=".NET框架">
<Author>Jim</Author>
<Price>40.00</Price>
</Book>
<Book BookName="精通C#">
<Author>Tom</Author>
<Price>60.00</Price>
</Book>
<Book BookName="ASP.NET">
<Author>David</Author>
<Price>100.00</Price>
</Book>
<Book BookName="Windows Application">
<Author>Jack</Author>
<Price>70.00</Price>
</Book>
</Books>
如果要删除节点属性请使用RemoveAttribute方法,如果要删除节点请使用RemoveChild方法,也可以使用RemoveAll方法删除所有节点。关于这类方法的使用不再赘述。
另外DataSet也可以读写XML文件:
// 方法有重载
ds.readxml(Server.MapPath( " txt.xml " ));
ds.WriteXml(Server.MapPath( ));
将字符串保存为XML文件
doc.LoadXml(txtXMLEditor.Text);
doc.Save(Server.MapPath( XMLFile.xml ));
C#创建XML文档、增删节点类:
System.Collections.Generic;
System.Linq;
System.Text;
System.Xml;
System.IO;
namespace OperateXml
{
public class XmlHelper
{
/// <summary>
创建XML文件
</summary> <param name="fileName"> XML文件路径和文件名 </param> <param name="rootElement"> XML文件根元素名称 </param>
static void CreateXmlFile( string fileName, rootElement)
{
if ( ! File.Exists(fileName))
{
创建XML文件 XmlTextWriter writer XmlTextWriter(fileName, Encoding.UTF8);
设置自动缩进 writer.Formatting Formatting.Indented;
XML版本声明 writer.WriteStartDocument();
根元素 writer.WriteStartElement(rootElement);
关闭根元素,并书写结束标签 writer.WriteEndElement();
将XML写入文件并且关闭XmlTextWriter实例对象 writer.Close();
}
}
添加XML数据
<param name="itemElement"> XML文件二级元素名称 <param name="childElement"> XML文件三级元素名称和值 AppendChildElement( rootElement,0)"> itemElement, Dictionary < ,0)">> childElement)
{
XmlDocument xmlDoc XmlDocument();
xmlDoc.Load(fileName);
XmlNode root xmlDoc.SelectSingleNode(rootElement);
XmlElement el xmlDoc.CreateElement(itemElement);
foreach (var item in childElement)
{
XmlElement elChild xmlDoc.CreateElement(item.Key);
elChild.InnerText item.Value;
el.AppendChild(elChild);
}
root.AppendChild(el);
xmlDoc.Save(fileName);
}
删除数据行
XML文件 <param name="rowNode"> 顶级节点 <param name="elementID"> 要查找的三级节点的InnerText,根据此值删除二级节点(一条数据) RemoveRecord( rowNode,0)"> elementID)
{
XmlDocument xmlDoc XmlDocument();
xmlDoc.Load(fileName);
XmlNodeList nodeList xmlDoc.SelectSingleNode(rowNode).ChildNodes;
(XmlNode node nodeList)
{
XmlElement el (XmlElement)node;
XmlNodeList nodeListChild el.ChildNodes;
(XmlNode nodeChild nodeListChild)
{
(nodeChild.InnerText == elementID)
{
xmlDoc.SelectSingleNode(rowNode).RemoveChild(node);
break ; } } } xmlDoc.Save((fileName)); } } }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。