XML创建、读、写操作
Create:
int nFQ;
XmlDocument doc = new XmlDocument();
XmlAttribute
newAtt;
//定义XML文档头文件
XmlDeclaration dec =
doc.CreateXmlDeclaration("1.0",null,null);
doc.AppendChild(dec);
XmlElement
docRoot = doc.CreateElement("Orders");
doc.AppendChild(docRoot);
for(int i=0;i<12;i++)
{
XmlNode Order =
doc.CreateElement("Order");
newAtt =
doc.CreateAttribute("Quantity");
nFQ = 10*i +i;
newAtt.Value
= nFQ.ToString();
Order.Attributes.Append(newAtt);
docRoot.AppendChild(Order);
}
//
保存XML文档
string strPath = Server.MapPath("OutDocument.XML");
doc.Save(strPath);
Read:
一:
// 创建XmlDocument类的实例
XmlDocument doc = new XmlDocument();
ArrayList
NodeValues = new ArrayList();
// 把people.xml文件读入内存,形成一个DOM结构
doc.Load(
Server.MapPath("people.xml") );
XmlNode root =
doc.DocumentElement;
foreach( XmlNode personElement in
root.ChildNodes )
//吧节点加入数组
NodeValues.Add(personElement.FirstChild.Value);
//在ListBox中显示
XMLNodeListBox.DataSource = NodeValues;
XMLNodeListBox.DataBind();
二:
//读取XML到DataSet
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath(".\\db\\dbGuest.xml"));
GridView1.Dat
相关文档:
一 DOM 以下代码用DOM是生成一个XML文档
package xml;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
imp ......
一、测试用的em.xml
<?xml version="1.0" encoding="GB2312"?>
<EW cmd="login" mod="Login" version="6.0">
<Source uns="" type="user"/>
<Username>zhangzhiyun@hp</Username>
<Password>111111</Password> ......
实例说明会更清楚些,假设有如下XML文件:
File: message_hutaow.xml
<?xml version="1.0" encoding="UTF-8"?>
<hutaow:Message version="1.0" xmlns:hutaow="http://wangtao.cublog.cn">
<hutaow:Head>
<hutaow:Date>20080502</hutaow:Date>
<hutaow:Source>
......
工作中遇到需要合并XML的问题,遂从网上查找相关资料,
1、 《 XML merging made easy
》
2 、《java中合并xml文档的设计与实现
》
测试后发现均不符合实际需求,实际需求如下:
file1.xml:
<root>
<a>
<b name="1"/>
</a>
<d /& ......