如何对任意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();
        }

以上是“如何对任意XML进行遍历”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


本文题目:如何对任意XML进行遍历
文章出自:http://cdiso.cn/article/ihpoos.html

其他资讯