📄 xml-via-navigator.aspx
字号:
<%@Page Language="C#" %>
<%@Import Namespace="System.Xml" %>
<%@Import Namespace="System.Xml.XPath" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html><head>
<title>Accessing Xml documents using an XPathNavigator</title>
<!-- #include file="..\global\style.inc" -->
</head>
<body bgcolor="#ffffff">
<span class="heading">Accessing Xml documents using an XPathNavigator</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";
// declare a variable to hold an XmlDocument object
XmlDocument objXmlDoc = new XmlDocument();
try
{
// create a new XpathDocument object and load the Xml file
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 new XPathNavigator object using the XmlDocument object
XPathNavigator objXPNav = objXmlDoc.CreateNavigator();
// move the current position to the root #document node
objXPNav.MoveToRoot();
// call a recursive function to iterate through all the nodes in the
// XPathNavigator, creating a string that is placed in the <div> above
outResults.InnerHtml = GetXmlDocFragment(objXPNav, 0);
}
string GetXmlDocFragment(XPathNavigator objXPNav, int intLevel)
{
string strNodes = "";
int intLoop;
// display information about this node
strNodes = strNodes + GetIndent(intLevel)
+ GetNodeType(objXPNav.NodeType) + ": <b>" + objXPNav.Name
+ "</b> value=<b>" + objXPNav.Value + "</b><br />";
// see if this node has any Attributes
if (objXPNav.HasAttributes)
{
// move to the first attribute
objXPNav.MoveToFirstAttribute();
do
{
// display the information about it
strNodes = strNodes + GetIndent(intLevel + 1)
+ GetNodeType(objXPNav.NodeType) + ": <b>" + objXPNav.Name
+ "</b> value=<b>" + objXPNav.Value + "</b><br />";
}
while (objXPNav.MoveToNextAttribute());
// then move back to the parent node (i.e. the element itself)
objXPNav.MoveToParent();
}
// see if this node has any child nodes
if (objXPNav.HasChildren)
{
// move to the first child node of the current node
objXPNav.MoveToFirstChild();
do
{
// recursively call this function to display the child node fragment
strNodes = strNodes + GetXmlDocFragment(objXPNav, intLevel + 1);
}
while (objXPNav.MoveToNext());
// move back to the parent node - the node we started from when we
// moved to the first child node - could have used Push and Pop instead
objXPNav.MoveToParent();
}
// must repeat the process for the remaining sibling nodes (i.e.nodes
// at the same // level' as the current node within the Xml document
// so repeat while we can move to the next sibling node
while (objXPNav.MoveToNext())
{
// recursively call this function to display this sibling node
// and its atributes and child nodes
strNodes = strNodes + GetXmlDocFragment(objXPNav, intLevel);
}
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(XPathNodeType 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 + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -