在.net平台下,XML处理技术有几种方式,主要分为2部分,写XML和读XML。 1 写XML,.net framework提供XMLTextWriter类来操作,把数据写入一个XML文件,代码如下: protected void CreateXMLFileButton_Click(object sender, System.EventArgs e) // 创建XmlDocument类的实例 XmlTextReader reader = new XmlTextReader( Server.MapPath(“students.xml”) ); while( reader.Read() ) 4 使用XPath XPathDocument xpdoc = new XPathDocument( Server.MapPath(“people.xml”) ); while (iterator.MoveNext()) XMLNodeListBox.DataSource = NodeValues; System.Xml.XmlDocument xmldoc = new System.Xml.XmlDocument(); <?xml version=’1.0′?> 来源:CSDN
\n
\n
\n
static void WriteXMLbyXmlWriter(XmlWriter writer, string symbol, double price, double change, long volume)
{
writer.WriteStartElement(“Stock”);
writer.WriteAttributeString(“Symbol”, symbol);
writer.WriteElementString(“Price”, XmlConvert.ToString(price));
writer.WriteElementString(“Change”, XmlConvert.ToString(change));
writer.WriteElementString(“Volume”, XmlConvert.ToString(volume));
writer.WriteEndElement();
}
\n
{
// 创建一个表示所要生成的XML文件路径的字符串。如果该路径指向NTFS分区,则需要相关的访问权限。
string filename = XMLFilePathTextBox.Text;
// 创建一个写入XML数据的文件流
System.IO.FileStream myFileStream = new System.IO.FileStream(filename, System.IO.FileMode.Create);
// 使用文件流对象创建一个XmlTextWriter对象
XmlTextWriter myXmlWriter = new XmlTextWriter(myFileStream, System.Text.Encoding.Unicode);
myXmlWriter.Formatting = Formatting.Indented;
try
{
// 使用WriteXMLbyXmlWriter方法把数据写入XmlTextWriter对象中
WriteXMLbyXmlWriter(myXmlWriter, “MSFT”, 74.5, 5.5, 49020000);
// 通过Close方法的调用,XmlTextWriter对象的数据最终写入XML文件
myXmlWriter.Close();
Page.Response.Write(“生成XML文档成功!”);
}
catch
{
Page.Response.Write(“生成XML文档失败!请检查路径是否正确,以及是否有写入权限。”);
}
}
2 把一个XML文件读入内存,形成一个DOM结构,把XML读入一个ArrayList
\n
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 )//root.ChildNodes获取节点的所有子节点
NodeValues.Add(personElement.FirstChild.Value);
3 使用XMLTextReader.读入ArrayList
\n
ArrayList NodeValues = new ArrayList();
\n
{
if( reader.NodeType == XmlNodeType.Element && reader.Name == “NAME” )
{
reader.Read();
NodeValues.Add( reader.Value );
}
}
\n
\n
XPathNavigator nav = xpdoc.CreateNavigator();
XPathExpression expr = nav.Compile(“descendant::PEOPLE/PERSON”);
XPathNodeIterator iterator = nav.Select(expr);
ArrayList NodeValues = new ArrayList();
\n
NodeValues.Add(iterator.Current.ToString());
\n
XMLNodeListBox.DataBind();
5 使用xsl展示XML中的数据
\n
xmldoc.Load(Server.MapPath(“user.xml”));
System.Xml.Xsl.XslTransform xmltrans = new System.Xml.Xsl.XslTransform();
xmltrans.Load(Server.MapPath(“user.xsl”));
Xml1.Document = xmldoc;
Xml1.Transform = xmltrans;
user.xsl:
\n
<xsl:stylesheet xmlns:xsl=”http://www.w3.org/1999/XSL/Transform” version=”1.0″>
<xsl:template match=”/”>
<HTML>
<BODY>
<FONT face=”Verdana” size=”3″>
<TABLE cellspacing=”5″ cellpadding=”1″>
<TR bgcolor=”#AAAAAA”>
<TD class=”header”><B>UserName</B></TD>
<TD class=”header”><B>Name</B></TD>
<TD class=”header”><B>Phone</B></TD>
<TD class=”header”><B>Address</B></TD>
</TR>
<xsl:for-each select=”Users/user”>
<TR bgcolor=”#DDDDDD”>
<TD>
<xsl:value-of select=”user_name”/>
</TD>
<TD>
<xsl:value-of select=”true_name”/>
</TD>
<TD>
<xsl:value-of select=”phone”/>
</TD>
<TD>
<xsl:value-of select=”address”/>
</TD>
</TR>
</xsl:for-each>
</TABLE>
</FONT>
</BODY>
</HTML>
</xsl:template>
</xsl:stylesheet>
\n