對于任意的XML的遍歷_Xml教程
推薦:如何從xml中獲取城市,省份名稱最近沒事,寫了個在項目經常要取城市或省份名的方法,所以改成了一個類.方便以后調用//********************************************************************************//*
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();
}
分享:使用純HTML的通用數據管理和服務使用純HTML的通用數據管理和服務。然而,為了收集數據,你需要一個數據儲存庫。要避免使用數據庫服務器帶來的很多問題,你可以在XML中收集這些數據。下面是我們的項目的基本結構: <user&g
- 相關鏈接:
- 教程說明:
Xml教程-對于任意的XML的遍歷。