class test
{
private static string root;
public static void showXML(string path)
{
XmlDocument xd = new XMLDocument();
xd.Load(path);
XMLNodeList xnl = xd.DocumentElement.ChildNodes;
root = xd.FirstChild.NextSibling.Name;//記錄根節點
Console.Write(root+"\n");
foreach (XMLNode xn in xnl)
{
//Console.Write(xn.Attributes["name"].Value.ToString()+"\n");
XMLNode child = xn.FirstChild;
NodeOperate(child);
}
}
public static void NodeOperate(XMLNode xn1)
{
if (xn1.HasChildNodes == true)
{
Console.Write(xn1.Name + "\n");
Console.Write("\n");
XMLNode childNode = xn1.FirstChild;
NodeOperate(childNode);
}
else
{
Console.Write(xn1.Name + "\n");
Console.Write(xn1.InnerText);
Console.Write("\n");
if (xn1.NextSibling != null)
{
NodeOperate(xn1.NextSibling);
}
else
{
int flag = 0;
while (xn1.NextSibling == null)
{
if (xn1.Name == root)//檢查是否到了根節點,如果不檢查會出現節點的引用錯誤
{
flag = 1;
break;
}
else
{
xn1 = xn1.ParentNode;
}
}
if (flag == 0)
{
NodeOperate(xn1.NextSibling);
}
else if(flag==1)
{
Console.Write("End");
}
}
}
}
}
public static void Main()
{
test.showXML(@"C:\Documents and Settings\SKY\My Documents\Visual Studio 2005\Projects\Project1\Project1\system.XML");
Console.Read();
&nb
sp; }