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
相关文档:
对于带有表空间xmlns的xml文件的解析,用正常解析文件的方法总是失效,不起作用,无法获得元素。
下面给出两种方法解析此类文件:
1.按正常解析xml文件的方法,需要注意几点:
获取元素Element,不可使用函数:document.selectNodes("//region");
只可以先取到根元素,一级一级往下取,eg:
Element root = document.g ......
1.要解析的XML文件如下:
<?xml version="1.0" encoding="GB2312"?>
<?xml-stylesheet href="student.xsl" type="text/xsl"?>
<StudentInfo>
<student>
<name>达内</name>
<sex>男</sex>
<lesson>
  ......
一、测试用的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> ......
JAXBContext jc = JAXBContext.newInstance(ICPBuildSummaryXO.class);
Unmarshaller u = jc.createUnmarshaller();
ICPBuildSummaryXO xo = (ICPBuildSummaryXO) u.unmarshal(node);
ICPBuildSummary summary = new ICPBuildSummary();
Bean ......
实例说明会更清楚些,假设有如下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>
......