<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="XmlDOM" %>
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 XmlDOM : 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);
XmlText.Text = GetChildNodesDescr(doc.ChildNodes, 0);
}
private string GetChildNodesDescr(XmlNodeList nodeList, int level)
{
string indent = "";
for (int i = 0; i < level; i++)
indent += " ";
StringBuilder str = new StringBuilder("");
foreach (XmlNode node in nodeList)
{
switch (node.NodeType)
{
case XmlNodeType.XmlDeclaration:
str.Append("XML Declaration: ");
str.Append(node.Name);
str.Append(" ");
str.Append(node.Value);
str.Append("
");
break;
case XmlNodeType.Element:
str.Append(indent);
str.Append("Element: ");
str.Append(node.Name);
str.Append("
");
break;
case XmlNodeType.Text:
str.Append(indent);
str.Append(" - Value: ");
str.Append(node.Value);
str.Append("
");
break;
case XmlNodeType.Comment:
str.Append(indent);
str.Append("Comment: ");
str.Append(node.Value);
str.Append("
");
break;
}
if (node.Attributes != null)
{
foreach (XmlAttribute attrib in node.Attributes)
{
str.Append(indent);
str.Append(" - Attribute: ");
str.Append(attrib.Name);
str.Append(" Value: ");
str.Append(attrib.Value);
str.Append("
");
}
}
if (node.HasChildNodes)
str.Append(GetChildNodesDescr(node.ChildNodes, level + 1));
}
return str.ToString();
}
}
File: Data.xml
title 1
directory 2
1
star 1
star 2
title 2
directory 2
2
star 3
star 4