search-dom.aspx

来自「Professional ASP.NET source code」· ASPX 代码 · 共 68 行

ASPX
68
字号
<%@Page Language="C#" %>
<%@Import Namespace="System.Xml" %>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Searching an Xml document using the DOM</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Searching an Xml document using the DOM</span><hr />
<!--------------------------------------------------------------------------->

<div id="outDocURL" runat="server"></div>
<div id="outError" runat="server">&nbsp;</div>
<div id="outResults" runat="server"></div>

<script language="C#" runat="server">

	void Page_Load(Object sender, EventArgs e)
	{
		// create physical path to booklist.xml sample file (in same folder as ASPX page)
		string strCurrentPath = Request.PhysicalPath;
		string strXmlPath = strCurrentPath.Substring(0, strCurrentPath.LastIndexOf("\\")) + "\\booklist.xml";

		// create a new XmlDocument object
		XmlDocument objXmlDoc = new XmlDocument();

		try
		{
			// load the Xml file into the XmlDocument object
			objXmlDoc.Load(strXmlPath);
			outDocURL.InnerHtml = "Loaded file: <b>" + strXmlPath + "</b>";
		}
		catch (Exception objError)
		{
			// display error details
			outError.InnerHtml = "<b>* Error while accessing document</b>.<br />"
							+ objError.Message + "<br />" + objError.Source;
			return; //  and stop execution
		}

		// now ready to parse the Xml document
		// it must be well-formed to have loaded without error

		// create a string to hold the matching values found
		string strResults = "<b>List of authors</b>:<br />";

		// create a NodeList collection of all matching child nodes
		XmlNodeList colElements;
		colElements = objXmlDoc.GetElementsByTagName("AuthorName");

		// iterate through the collection getting the values of the
		// child #text nodes for each one
		foreach (XmlNode objNode in colElements)
			strResults += objNode.FirstChild.Value + "<br />";

		// then display the result
		outResults.InnerHtml = strResults ;  // display the result

	}

</script>

<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?