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();
}
以上就是对于任意的XML的遍历的内容,更多相关内容请关注PHP中文网(www.php.cn)!
声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
程序员必备接口测试调试工具:点击使用
Apipost = Postman + Swagger + Mock + Jmeter
Api设计、调试、文档、自动化测试工具
网页生成APP,用做网站的技术去做APP:立即创建
手机网站开发APP、自助封装APP、200+原生模块、2000+映射JS接口按需打包
相关文章
相关视频