<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="XPathNavigatorRead" %>
Untitled Page
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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.Xml.XPath;
using System.Text;
public partial class XPathNavigatorRead : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string xmlFile = Server.MapPath("Data.xml");
XmlDocument doc = new XmlDocument();
doc.Load(xmlFile);
XPathNavigator xnav = doc.CreateNavigator();
XmlText.Text = GetXNavDescr(xnav, 0);
}
private string GetXNavDescr(XPathNavigator xnav, int level)
{
string indent = "";
for (int i = 0; i < level; i++)
indent += " ";
StringBuilder str = new StringBuilder("");
switch (xnav.NodeType)
{
case XPathNodeType.Root:
str.Append("ROOT");
str.Append("
");
break;
case XPathNodeType.Element:
str.Append(indent);
str.Append("Element: ");
str.Append(xnav.Name);
str.Append("
");
break;
case XPathNodeType.Text:
str.Append(indent);
str.Append(" - Value: ");
str.Append(xnav.Value);
str.Append("
");
break;
case XPathNodeType.Comment:
str.Append(indent);
str.Append("Comment: ");
str.Append(xnav.Value);
str.Append("
");
break;
}
if (xnav.HasAttributes)
{
xnav.MoveToFirstAttribute();
do
{
str.Append(indent);
str.Append(" - Attribute: ");
str.Append(xnav.Name);
str.Append(" Value: ");
str.Append(xnav.Value);
str.Append("
");
} while (xnav.MoveToNextAttribute());
xnav.MoveToParent();
}
if (xnav.HasChildren)
{
xnav.MoveToFirstChild();
do
{
str.Append(GetXNavDescr(xnav, level + 1));
} while (xnav.MoveToNext());
xnav.MoveToParent();
}
return str.ToString();
}
}
File: Data.xml
title 1
directory 2
1
star 1
star 2
title 2
directory 2
2
star 3
star 4