Viewing source for recipe1207cs.aspx

<%@ 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("users.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 = "&nbsp;&nbsp;&nbsp;";
		String space = "&nbsp;";
        while (_XmlReader.Read())
        {
            switch(_XmlReader.NodeType)
            {
              case XmlNodeType.Element:
                  sb.Append("<B>Element: </B>");
                  sb.Append(_XmlReader.Name);
                  sb.Append("<BR/>");
              
                  //Print attributes for current node, if any available...
                  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">
			<p>
				<asp:Literal id="OutputLiteral" runat="server" EnableViewState="False"></asp:Literal>
			</p>
			<p>
				<asp:Label id="ErrorLabel" runat="server" ForeColor="Red"></asp:Label>
			</p>
		</form>
	</body>
</html>