对于任意的XML的遍历_XHTML/WEB_黑客防线网安服务器维护基地--Powered by WWW.RONGSEN.COM.CN

对于任意的XML的遍历

作者:黑客防线网安XML教程基地 来源:黑客防线网安XML教程基地 浏览次数:0

本篇关键词:任意对于 XmlNode
黑客防线网安网讯:  class test          {              private static string root;              public static  void showXML(string path)              {                  XmlDocument x...

  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+" ");

                  foreach (XmlNode xn in xnl)
                  {
                      //Console.Write(xn.Attributes["name"].Value.ToString()+" ");
                      XmlNode child = xn.FirstChild;
                     
                      NodeOperate(child);
                

                  }
              }

              public static  void NodeOperate(XmlNode xn1)
              {

                  if (xn1.HasChildNodes == true)
                  {
                      Console.Write(xn1.Name + " ");
                      Console.Write(" ");
                      XmlNode childNode = xn1.FirstChild;
                     
                      NodeOperate(childNode);

                  }
                  else
                  {
                      Console.Write(xn1.Name + " ");
                      Console.Write(xn1.InnerText);
                      Console.Write(" ");
                      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 SettingsSKYMy DocumentsVisual Studio 2005ProjectsProject1Project1system.xml");
              Console.Read();

          }

    黑客防线网安服务器维护方案本篇连接:http://www.rongsen.com.cn/show-15926-1.html
网站维护教程更新时间:2012-04-06 01:01:39  【打印此页】  【关闭
我要申请本站N点 | 黑客防线官网 |  
专业服务器维护及网站维护手工安全搭建环境,网站安全加固服务。黑客防线网安服务器维护基地招商进行中!QQ:29769479

footer  footer  footer  footer