C# 遍历XML,可执行

 
  1. class XMLTester
  2.     {
  3.         #region 准备处理XML片段
  4.         /// <summary>
  5.         /// 
  6.         /// </summary>
  7.         /// <param name="fileFullPath"></param>
  8.         public void processXML(string fileFullPath)
  9.         {
  10.             XmlDocument xmlDoc = new XmlDocument();
  11.             //给定了文件的全路径,载入文档
  12.             xmlDoc.Load(fileFullPath);
  13.             //若给定的不是文件,而是XML字符串,可以如下得到Document
  14.             //xmlDoc.LoadXml(xmlStr);
  15.             // 此处是处理文档中的 //Resources//UIResource节点,这是一个XML片段
  16.             // XmlNode node = xmlDoc.DocumentElement.SelectSingleNode("//Resources//UIResource");
  17.             
  18.             // 也可以处理整篇文档
  19.             XmlNode node = xmlDoc.DocumentElement;
  20.             string rootString = node.Name;
  21.             processNode(node, rootString);
  22.         }
  23.         public void processNode(XmlNode xmlNode,string rootStr)
  24.         {
  25.             if (xmlNode.HasChildNodes == true)
  26.             {
  27.                 processBranchNode(xmlNode);
  28.                 processNode(xmlNode.FirstChild, rootStr);
  29.             }
  30.             else
  31.             {
  32.                 processLeafNode(xmlNode);
  33.                 if (xmlNode.NextSibling != null)
  34.                 {
  35.                     processNode(xmlNode.NextSibling,rootStr);
  36.                 }
  37.                 else
  38.                 {
  39.                     int flag = 0;
  40.                     while (xmlNode.NextSibling == null)
  41.                     {
  42.                         if (xmlNode.Name == rootStr)//检查是否到了根节点,如果不检查会出现节点的引用错误
  43.                         {
  44.                             flag = 1;
  45.                             break;
  46.                         }
  47.                         else
  48.                         {
  49.                             xmlNode = xmlNode.ParentNode;
  50.                         }
  51.                     }
  52.                     if (flag == 0)
  53.                     {
  54.                         if (xmlNode.Name != rootStr)
  55.                             processNode(xmlNode.NextSibling,rootStr);
  56.                         else
  57.                             Console.Write("process XML End=============================/r/n/r/n");
  58.                     }
  59.                     else if (flag == 1)
  60.                     {
  61.                         Console.Write("process XML End=============================/r/n/r/n");
  62.                     }
  63.                 }
  64.             }
  65.         }
  66.         /// <summary>
  67.         ///处理叶节点,可用 
  68.         /// </summary>
  69.         /// <param name="xmlNode"> 一个叶节点</param>
  70.         private static void processLeafNode(XmlNode xmlNode)
  71.         {
  72.             Console.Write("   "+xmlNode.Name);
  73.             Console.WriteLine("  is a leaf XML Node ,its first Attribute: key:"+xmlNode.Attributes[0].Name +";   Value:"+xmlNode.Attributes[0].Value);            
  74.             Console.Write(xmlNode.InnerText);
  75.             Console.Write("/n");
  76.         }
  77.         private static void processBranchNode(XmlNode xmlNode)
  78.         {
  79.             Console.Write(xmlNode.Name);
  80.             Console.WriteLine("  has children; children count:  " +xmlNode.ChildNodes.Count);            
  81.             Console.Write("/n");
  82.         }
  83.         #endregion
  84.        
  85.            public static void Main()
  86.         {
  87.             XMLTester t = new XMLTester();
  88.             t.processXML(@"D:/test.xml");
  89.             Console.Read();
  90.         }
  91.     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值