对于任意的XML的遍历

来源:互联网 时间:2008-09-05 17:11:00 

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();

        }

标签:
0
投稿

猜你喜欢

  • PHP图片上传类带图片显示

    2024-05-22 10:06:19
  • 浅谈java里的EL表达式在JSP中不能解析的问题

    2023-06-20 11:55:05
  • sqlserver2005利用临时表和@@RowCount提高分页查询存储过程性能示例分享

    2024-01-21 05:41:06
  • 彻底卸载MySQL的方法分享

    2024-01-23 11:49:08
  • 解决“引入同一个JS文件在非IE6中正常,但IE6报错”的问题

    2009-04-03 11:42:00
  • ASP UTF-8页面乱码+GB2312转UTF-8 +生成UTF-8格式的文件(编码)

    2011-04-19 11:15:00
  • python实现机器学习之元线性回归

    2022-05-30 06:02:54
  • 对numpy下的轴交换transpose和swapaxes的示例解读

    2022-09-11 10:30:51
  • Python学习之列表常用方法总结

    2021-11-24 18:43:19
  • mysql存储过程基础之遍历多表记录后插入第三方表中详解

    2024-01-24 12:59:27
  • 压缩技术给SQL Server备份文件瘦身

    2024-01-26 05:10:23
  • MySQL备份脚本的写法

    2024-01-18 01:35:08
  • XHTML1.0规范:您是否为img图片标签赋予alt属性

    2009-09-21 11:11:00
  • Python爬虫突破反爬虫机制知识点总结

    2021-09-17 12:38:22
  • PHP mysql_result()函数使用方法

    2023-06-13 08:21:29
  • 仿dw8代码折叠功能的HTML编辑器

    2008-04-29 21:19:00
  • 一条select语句引起的瓶颈问题思考

    2024-01-18 02:40:32
  • Pandas提高数据分析效率的13个技巧汇总

    2022-09-19 14:23:25
  • Go语言依赖管理三要素示例解析

    2024-05-05 09:32:34
  • Python爬虫之网络请求

    2023-10-29 21:28:08
  • asp之家 网络编程 m.aspxhome.com