概述
对于任意的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 SettingsSKYMy DocumentsVisual Studio 2005ProjectsProject1Project1system.xml");
Console.Read();
}
登录后复制
以上就是对于任意的XML的遍历的内容,更多相关内容请关注靠谱客(www.uoften.com)!
最后
以上就是风趣荷花为你收集整理的对于任意的XML的遍历的全部内容,希望文章能够帮你解决对于任意的XML的遍历所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复