在网上找了半天找到本英文的<<Professional ASP.NET 2.0 XML>>.无奈之下,对起进行了学习,特把一些例子记录下来,供参考.主要是中文翻译的都要钱!
<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
<book genre="autobiography">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Data.SqlClient;
/// <summary>
/// 功能:通过<<Professional ASP.NET2.0 XML-Chapter Six>>上面的例子学习XML
/// 编码:邓古谚 flyin2006
/// 时间:2006.9.15
/// </summary>

public partial class _Default : System.Web.UI.Page
{
#region Page_Load() 例一到例四都是读xml ;
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("例子一:加载xml数据,然后调用显示函数"+"</br>");
TwoLoadXmlExample();

Response.Write("</br>");
Response.Write("</br>" + "例子二:显示xml全部节点" + "</br>");
GetAllNodes();
Response.Write("</br>"+"例子三:显示xml特定节点"+"</br>");
GetSpecificallyNodes();
Response.Write("</br>" + "例子四:操作某一节点下的子树用XmlNodeReader方法" + "</br>");
OperationSubTreeUsingXmlNodeReader();
}
#endregion

#region 例子四:操作某一节点下的子树用XmlNodeReader方法 SubTree
protected void OperationSubTreeUsingXmlNodeReader()
{
string xmlPath = Request.PhysicalApplicationPath + @"/App_Data/Books.xml";
XmlDocument booksDocument = new XmlDocument();
booksDocument.Load(xmlPath);
XmlNode node = booksDocument.SelectSingleNode("/bookstore/book[@genre='autobiography']");
XmlNodeReader nodeReader = new XmlNodeReader(node);
while (nodeReader.Read())
{
if (nodeReader.NodeType == XmlNodeType.Element)
{
Response.Write("NodeType is Element: NodeName="+nodeReader.Name.ToString()+"</br>");
}
if (nodeReader.NodeType == XmlNodeType.Text)
{
Response.Write("NodeType is Text: NodeName=" + nodeReader.Value.ToString() + "</br>");
}
}
}
#endregion

#region 例子三:显示xml特定节点 Example:Using Method GetElementsByTagName
protected void GetSpecificallyNodes()
{
string xmlPath = Request.PhysicalApplicationPath + @"/App_Data/Books.xml";
XmlDocument doc = new XmlDocument();
doc.Load(xmlPath);
//Get all job titles in the XML file
XmlNodeList titleList = doc.GetElementsByTagName("title");
Response.Write("Titles: " + "<br>");
foreach (XmlNode node in titleList)
{
Response.Write("Title : " + node.FirstChild.Value + "<br>");
}
//Get reference to the first author node in the XML file
XmlNode authorNode = doc.GetElementsByTagName("author")[0];
foreach (XmlNode child in authorNode.ChildNodes)
{
if ((child.Name == "first-name") &&
(child.NodeType == XmlNodeType.Element))
{
Response.Write("First Name : " + child.FirstChild.Value + "<br>");
}
if ((child.Name == "last-name") &&
(child.NodeType == XmlNodeType.Element))
{
Response.Write("Last Name : " + child.FirstChild.Value + "<br>");
}
}
}
#endregion

#region 例子二:显示xml全部节点
protected void GetAllNodes()
{
string xmlPath = Request.PhysicalApplicationPath + @"/App_Data/books.xml";
XmlDocument booksDocument = new XmlDocument();
booksDocument.Load(xmlPath);
XmlNode xmlRootNode = booksDocument.DocumentElement;
GetChildNodes(xmlRootNode);
}
/// <summary>
/// 函数加载XmlNode的所有子节点的内容
/// </summary>
/// <param name="node"></param>
protected void GetChildNodes(XmlNode node)
{
if (node.NodeType == XmlNodeType.Text)
{
Response.Write("xmlType = "+node.NodeType.ToString()+" && xmlValue = "+ node.Value.ToString() + "</br>");
}
else
{
Response.Write("xmlType = " + node.NodeType.ToString() + " && xmlValue = " + node.Name.ToString() + "</br>");
}

if (node.Attributes != null)
{
XmlAttributeCollection xmlAttCollection = node.Attributes;
foreach(XmlAttribute xmlAtt in xmlAttCollection)
{
Response.Write("xmlAttName = " + xmlAtt.Name.ToString() + " && xmlAttValue = " + xmlAtt.Value.ToString() + "</br>");
}
}
XmlNodeList nodeList = node.ChildNodes;
foreach (XmlNode chldNode in nodeList)
{
GetChildNodes(chldNode);
}
}
#endregion

#region 例子一:提供两种加载XML文件的方法: 一:把.xml文件加载到XmlDocument; 二:把xml字符串加载到XmlDocument
protected void TwoLoadXmlExample()
{
string xmlPath = Request.PhysicalApplicationPath + @"/App_Data/books.xml";
string xmlPath2 = Request.PhysicalApplicationPath + @"/App_Data/employee.xml";
XmlDocument bookDocument = new XmlDocument();
XmlDocument employeeDocument = new XmlDocument();
//Response.ContentType = "text/xml";
try
{
bookDocument.PreserveWhitespace = true;
bookDocument.Load(xmlPath);
Response.Write(bookDocument.InnerXml);

string strEmpXml = "<Employee><firstName>Deng</firstName><secondName>yangyan</secondName></Employee> ";
employeeDocument.LoadXml(strEmpXml);
employeeDocument.Save(xmlPath2);
}
catch (XmlException eee)
{
Response.Write(eee.ToString());
}
catch (Exception ee)
{
Response.Write("以下是发生错误的原因 ");
Response.Write(ee.ToString());
}
}
#endregion
}
http://blog.csdn.net/flyin2006/archive/2006/09/17/1227708.aspx
这几个小例子是读XML文件,读XML文件的全部节点或部分节点等!都用XmlDocument类!
1.books.xml
<?xml version='1.0'?>
<!-- This file represents a fragment of a book store inventory database -->
<bookstore>
<book genre="autobiography">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>8.99</price>
</book>
<book genre="novel">
<title>The Confidence Man</title>
<author>
<first-name>Herman</first-name>
<last-name>Melville</last-name>
</author>
<price>11.99</price>
</book>
<book genre="philosophy">
<title>The Gorgias</title>
<author>
<name>Plato</name>
</author>
<price>9.99</price>
</book>
</bookstore>
2.Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.Data.SqlClient;
/// <summary>
/// 功能:通过<<Professional ASP.NET2.0 XML-Chapter Six>>上面的例子学习XML
/// 编码:邓古谚 flyin2006
/// 时间:2006.9.15
/// </summary>
public partial class _Default : System.Web.UI.Page
{
#region Page_Load() 例一到例四都是读xml ;
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("例子一:加载xml数据,然后调用显示函数"+"</br>");
TwoLoadXmlExample();
Response.Write("</br>");
Response.Write("</br>" + "例子二:显示xml全部节点" + "</br>");
GetAllNodes();
Response.Write("</br>"+"例子三:显示xml特定节点"+"</br>");
GetSpecificallyNodes();
Response.Write("</br>" + "例子四:操作某一节点下的子树用XmlNodeReader方法" + "</br>");
OperationSubTreeUsingXmlNodeReader();
}
#endregion
#region 例子四:操作某一节点下的子树用XmlNodeReader方法 SubTree
protected void OperationSubTreeUsingXmlNodeReader()
{
string xmlPath = Request.PhysicalApplicationPath + @"/App_Data/Books.xml";
XmlDocument booksDocument = new XmlDocument();
booksDocument.Load(xmlPath);
XmlNode node = booksDocument.SelectSingleNode("/bookstore/book[@genre='autobiography']");
XmlNodeReader nodeReader = new XmlNodeReader(node);
while (nodeReader.Read())
{
if (nodeReader.NodeType == XmlNodeType.Element)
{
Response.Write("NodeType is Element: NodeName="+nodeReader.Name.ToString()+"</br>");
}
if (nodeReader.NodeType == XmlNodeType.Text)
{
Response.Write("NodeType is Text: NodeName=" + nodeReader.Value.ToString() + "</br>");
}
}
}
#endregion
#region 例子三:显示xml特定节点 Example:Using Method GetElementsByTagName
protected void GetSpecificallyNodes()
{
string xmlPath = Request.PhysicalApplicationPath + @"/App_Data/Books.xml";
XmlDocument doc = new XmlDocument();
doc.Load(xmlPath);
//Get all job titles in the XML file
XmlNodeList titleList = doc.GetElementsByTagName("title");
Response.Write("Titles: " + "<br>");
foreach (XmlNode node in titleList)
{
Response.Write("Title : " + node.FirstChild.Value + "<br>");
}
//Get reference to the first author node in the XML file
XmlNode authorNode = doc.GetElementsByTagName("author")[0];
foreach (XmlNode child in authorNode.ChildNodes)
{
if ((child.Name == "first-name") &&
(child.NodeType == XmlNodeType.Element))
{
Response.Write("First Name : " + child.FirstChild.Value + "<br>");
}
if ((child.Name == "last-name") &&
(child.NodeType == XmlNodeType.Element))
{
Response.Write("Last Name : " + child.FirstChild.Value + "<br>");
}
}
}
#endregion
#region 例子二:显示xml全部节点
protected void GetAllNodes()
{
string xmlPath = Request.PhysicalApplicationPath + @"/App_Data/books.xml";
XmlDocument booksDocument = new XmlDocument();
booksDocument.Load(xmlPath);
XmlNode xmlRootNode = booksDocument.DocumentElement;
GetChildNodes(xmlRootNode);
}
/// <summary>
/// 函数加载XmlNode的所有子节点的内容
/// </summary>
/// <param name="node"></param>
protected void GetChildNodes(XmlNode node)
{
if (node.NodeType == XmlNodeType.Text)
{
Response.Write("xmlType = "+node.NodeType.ToString()+" && xmlValue = "+ node.Value.ToString() + "</br>");
}
else
{
Response.Write("xmlType = " + node.NodeType.ToString() + " && xmlValue = " + node.Name.ToString() + "</br>");
}
if (node.Attributes != null)
{
XmlAttributeCollection xmlAttCollection = node.Attributes;
foreach(XmlAttribute xmlAtt in xmlAttCollection)
{
Response.Write("xmlAttName = " + xmlAtt.Name.ToString() + " && xmlAttValue = " + xmlAtt.Value.ToString() + "</br>");
}
}
XmlNodeList nodeList = node.ChildNodes;
foreach (XmlNode chldNode in nodeList)
{
GetChildNodes(chldNode);
}
}
#endregion
#region 例子一:提供两种加载XML文件的方法: 一:把.xml文件加载到XmlDocument; 二:把xml字符串加载到XmlDocument
protected void TwoLoadXmlExample()
{
string xmlPath = Request.PhysicalApplicationPath + @"/App_Data/books.xml";
string xmlPath2 = Request.PhysicalApplicationPath + @"/App_Data/employee.xml";
XmlDocument bookDocument = new XmlDocument();
XmlDocument employeeDocument = new XmlDocument();
//Response.ContentType = "text/xml";
try
{
bookDocument.PreserveWhitespace = true;
bookDocument.Load(xmlPath);
Response.Write(bookDocument.InnerXml);
string strEmpXml = "<Employee><firstName>Deng</firstName><secondName>yangyan</secondName></Employee> ";
employeeDocument.LoadXml(strEmpXml);
employeeDocument.Save(xmlPath2);
}
catch (XmlException eee)
{
Response.Write(eee.ToString());
}
catch (Exception ee)
{
Response.Write("以下是发生错误的原因 ");
Response.Write(ee.ToString());
}
}
#endregion
}


浙公网安备 33010602011771号