xml-via-textreader.aspx

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

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

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Accessing an Xml document with an XmlTextReader object</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Accessing an Xml document with an XmlTextReader object</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";

		// declare a variable to hold an XmlTextReader object
		XmlTextReader objXmlReader;

		try
		{
			// create a new XmlTextReader object for the Xml file
			objXmlReader = new XmlTextReader(strXmlPath);
			outDocURL.InnerHtml = "Opened 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 read (or "pull") the nodes of the Xml document
		string strNodeResult = "";
		XmlNodeType objNodeType;

		// read each node in turn - returns False if no more nodes to read
		while (objXmlReader.Read())
		{
			// select on the type of the node (these are only some of the types)
			objNodeType = objXmlReader.NodeType;

			switch(objNodeType)
			{
			case XmlNodeType.XmlDeclaration:
				// get the name and value
				strNodeResult += "Xml Declaration: <b>" + objXmlReader.Name
								+ " " + objXmlReader.Value + "</b><br />";
				break;

			case XmlNodeType.Element:
				// just get the name, any value will be in next (#text) node
				strNodeResult += "Element: <b>" + objXmlReader.Name + "</b><br />";
				break;

			case XmlNodeType.Text:
				// just display the value, node name is "#text" in this case
				strNodeResult += "&nbsp; - Value: <b>" + objXmlReader.Value
								+ "</b><br />";
				break;
			}

			// see if this node has any attributes
			if (objXmlReader.AttributeCount > 0)
			{
				// iterate through the attributes by moving to the next one
				// could use MoveToFirstAttribute but MoveToNextAttribute does
				// the same when the current node is an element-type node
				while (objXmlReader.MoveToNextAttribute())
				{
					// get the attribute name and value
					strNodeResult +=  "&nbsp; - Attribute: <b>" + objXmlReader.Name
								+ "</b> &nbsp; Value: <b>" + objXmlReader.Value
								+ "</b><br />";
				}
			}
		}		// and read the next node

		// finished with the reader so close it
		objXmlReader.Close();

		// and display the results in the page
		outResults.InnerHtml = strNodeResult;

	}

</script>

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

⌨️ 快捷键说明

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