<%@ import Namespace="System" %>
<%@ import Namespace="System.Xml" %>
<%@ Page Language="C#" %>
<script runat="server">
XmlTextReader _Reader;
public void Page_Load(Object Source, EventArgs E)
{
try{
_Reader = new XmlTextReader(Server.MapPath("Data.xml"));
ReadDocument(_Reader);
}
catch (Exception _Error){
ErrorLabel.Text = _Error.Message;
}
finally{
_Reader.Close();
}
}
public void ReadDocument(XmlTextReader _XmlReader)
{
System.Text.StringBuilder sb = new System.Text.StringBuilder(100);
String space3 = " ";
String space = " ";
while (_XmlReader.Read())
{
switch(_XmlReader.NodeType)
{
case XmlNodeType.Element:
sb.Append("<B>Element: </B>");
sb.Append(_XmlReader.Name);
sb.Append("<BR/>");
if (_XmlReader.AttributeCount > 0)
{
while (_XmlReader.MoveToNextAttribute())
{
sb.Append(space3);
sb.Append("<B>Attribute Name: </B>");
sb.Append(_XmlReader.Name);
sb.Append(space);
sb.Append("<B>Attribute Value: </B>");
sb.Append(_XmlReader.Value);
sb.Append("<BR/>");
}
}
break;
case XmlNodeType.Text:
sb.Append(space3);
sb.Append("<B>Value: </B>");
sb.Append(_XmlReader.Value);
sb.Append("<BR/>");
break;
}
}
OutputLiteral.Text = sb.ToString();
}
</script>
<html>
<head>
<title>Using the XmlTextReader</title>
</head>
<body>
<form runat="server">
<asp:Literal id="OutputLiteral" runat="server" EnableViewState="False"></asp:Literal>
<asp:Label id="ErrorLabel" runat="server" ForeColor="Red"></asp:Label>
</form>
</body>
</html>
|