XML的简单读取与写入
已知有一个XML文件(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>
</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;//将子节点类型转换为
相关文档:
通过Asp.net(C#)应用程序读取本地上传的Excle文件,存放到DataSet中,通过DataSet中的方法直接生成XML文件.
C# Code
if (this.FileUpload1.PostedFile != null)
{
string filename = this.FileUpl ......
在针对XML文档的应用编程接口中,最主要的有W3C制定的DOM(Document Object Method,文档对象模型)和由David Megginson领导的SAX(SimpleAPI for XML,用于XML的简单API)[19]。
SAX和DOM在实现过程中,分别侧重于不同的方面以满足不同的应用需求。DOM为开发基于XML的应用系统提供了便利。它通过一种随机访问机制,使得应用程 ......
文章转自【PHP探路者】
XML 应用于 web 开发的许多方面,常用于简化数据的存储和共享。
如果需要在 HTML 文档中显示动态数据,那么每当数据改变时将花费大量的时间来编辑 HTML,
XML 可以把数据从 HTML 中分离开来。
通过 XML,数据能够存储在独立的 XML 文件中。这样你就可以专注于使用 HTML 进行布局和显示,并确保修 ......
XML (EXtensible Markup Language)
XML is a
cross-platform, software and hardware independent tool for transmitting
information.
Prolog
XML
Declaration
Processing
Instructions
DOCTYPE
Declaration
Elements
Attributes
XML
Comments
CDATA
Sections
Character
and Entity Refere ......