<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="WriteAndReadXml" %>
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.Text;
public partial class WriteAndReadXml : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string xmlFile = Server.MapPath("Data.xml");
XmlTextReader reader = new XmlTextReader(xmlFile);
StringBuilder str = new StringBuilder();
while (reader.Read())
{
switch (reader.NodeType)
{
case XmlNodeType.XmlDeclaration:
str.Append("XML Declaration: ");
str.Append(reader.Name);
str.Append(" ");
str.Append(reader.Value);
str.Append("
");
break;
case XmlNodeType.Element:
str.Append("Element: ");
str.Append(reader.Name);
str.Append("
");
break;
case XmlNodeType.Text:
str.Append(" - Value: ");
str.Append(reader.Value);
str.Append("
");
break;
case XmlNodeType.Comment:
str.Append("Comment: ");
str.Append(reader.Value);
str.Append("
");
break;
}
if (reader.AttributeCount > 0)
{
while (reader.MoveToNextAttribute())
{
str.Append(" - Attribute: ");
str.Append(reader.Name);
str.Append(" Value: ");
str.Append(reader.Value);
str.Append("
");
}
}
}
reader.Close();
XmlText.Text = str.ToString();
}
}
File: Data.xml
title 1
directory 2
1
star 1
star 2
title 2
directory 2
2
star 3
star 4