【编程开发】AspAsp.NetCGIPHPJspXMLPERLC++C#VCVBDelphiPowerBuilderJAVA汇编数据库编程移动开发其它语言

您现在的位置:首页 > 网络学院 > 编程开发 > XML > 对于任意的XML的遍历

对于任意的XML的遍历

来源: 作者: 日期:2006-12-26

【聚杰网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();

        }

评论   点击查看全部评论
您的评论参与,将为聚杰带来更大的动力!请不要吝啬!
快速回复
请使用文明语言让我们维护健康绿色网络环境!

匿名发表   验证码: