xml-via-dom.aspx
来自「东软内部材料(四)asp等相关的教学案例 」· ASPX 代码 · 共 148 行
ASPX
148 行
<%@Page Language="C#" %>
<%@Import Namespace="System.Xml" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Accessing Xml documents using the DOM</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Accessing Xml documents using the DOM</span><hr />
<!--------------------------------------------------------------------------->
<div id="outDocURL" runat="server"></div>
<div id="outError" runat="server"> </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
+ "<br />Path=" + strXmlPath;
return; // and stop execution
}
// now ready to parse the Xml document
// it must be well-formed to have loaded without error
// call a recursive function to iterate through all the nodes
// in the document creating a string that is placed in the <div> above
outResults.InnerHtml = GetChildNodes(objXmlDoc.ChildNodes, 0);
}
string GetChildNodes(XmlNodeList objNodeList, int intLevel)
{
string strNodes = "";
// iterate through all the child nodes for the current node
foreach (XmlNode objNode in objNodeList)
{
// display information about this node
strNodes = strNodes + GetIndent(intLevel)
+ GetNodeType(objNode.NodeType) + ": <b>" + objNode.Name;
// if it is an Xml Declaration node, display the // special' properties
if (objNode.NodeType == XmlNodeType.XmlDeclaration)
{
// cast the XmlNode object to an XmlDeclaration object
XmlDeclaration objXmlDec = (XmlDeclaration)objNode;
strNodes = strNodes + "</b> version=<b>" + objXmlDec.Version
+ "</b> standalone=<b>" + objXmlDec.Standalone + "</b><br />";
}
else
{
// just display the generic // value' property
strNodes = strNodes + "</b> value=<b>" + objNode.Value + "</b><br />";
}
// if it is an Element node, iterate through the Attributes
// collection displaying information about each attribute
if (objNode.NodeType == XmlNodeType.Element)
{
// display the attribute information for each attribute
foreach (XmlAttribute objAttr in objNode.Attributes)
{
strNodes = strNodes + GetIndent(intLevel + 1)
+ GetNodeType(objAttr.NodeType) + ": <b>" + objAttr.Name
+ "</b> value=<b>" + objAttr.Value + "</b><br />";
}
}
// if this node has child nodes, call the same function recursively
// to display the information for it and each of its child node
if (objNode.HasChildNodes)
strNodes = strNodes + GetChildNodes(objNode.ChildNodes, intLevel + 1);
}// go to next node
return strNodes; // pass the result back to the caller
}
string GetIndent(int intLevel)
{
// returns a string of non-breaking spaces used to indent each line
string strIndent = "";
for (int intIndent = 0; intIndent <= intLevel; intIndent++)
strIndent = strIndent + " ";
return strIndent;
}
string GetNodeType(XmlNodeType intType)
{
// returns the node type as a string
switch (Convert.ToInt32(intType))
{
case 0: return "NONE";
case 1: return "ELEMENT"; break;
case 2: return "ATTRIBUTE"; break;
case 3: return "TEXT"; break;
case 4: return "CDATA SECTION"; break;
case 5: return "ENTITY REFERENCE"; break;
case 6: return "ENTITY"; break;
case 7: return "PROCESSING INSTRUCTION"; break;
case 8: return "COMMENT"; break;
case 9: return "DOCUMENT"; break;
case 10: return "DOCUMENT TYPE"; break;
case 11: return "DOCUMENT FRAGMENT"; break;
case 12: return "NOTATION"; break;
case 13: return "WHITESPACE"; break;
case 14: return "SIGNIFICANT WHITESPACE"; break;
case 15: return "END ELEMENT"; break;
case 16: return "END ENTITY"; break;
case 17: return "Xml DECLARATION"; break;
case 18: return "NODE (ALL)"; break;
default: return "UNKNOWN"; break;
}
}
</script>
<!--------------------------------------------------------------------------->
<!-- #include file="..\global\foot.inc" -->
</body>
</html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?