如何对任意XML进行遍历-创新互联-成都快上网建站

如何对任意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://kswjz.com/article/cepshe.html
扫二维码与项目经理沟通

我们在微信上24小时期待你的声音

解答本文疑问/技术咨询/运营咨询/技术建议/互联网交流