温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

如何对任意XML进行遍历

发布时间:2021-09-17 11:14:13 来源:亿速云 阅读:106 作者:小新 栏目:编程语言

小编给大家分享一下如何对任意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进行遍历”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注亿速云行业资讯频道!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

xml
AI