C#中读取xml文件指定节点

  • View Code
     1 01.class Program  
     2 02.   {  
     3 03.       static void Main(string[] args)  
     4 04.       {  
     5 05.           XMLOperation xmlOpr = new XMLOperation();  
     6 06.           XmlNode xn=xmlOpr.getXmlNode("activity.xml", "2");  
     7 07.           string ActivityId = (xn.SelectSingleNode("ActivityId")).InnerText;  
     8 08.           string ActivityName = (xn.SelectSingleNode("ActivityName ")).InnerText;  
     9 09.           string ActivityLevel = xn.SelectSingleNode("ActivityLevel").InnerText;  
    10 10.           Console.WriteLine("ActivityId:" + ActivityId + "/nActivityName:" + ActivityName + "/nActivityLevel:" + ActivityLevel);  
    11 11.           Console.Read();  
    12 12.       }  
    13 13.   }  

     

    XmlDocumentSelectSingleNode方法的使用
  • XmlDocumentSelectNodes方法的使用
  • 通过节点属性查找指定节点

假设xml文件内容是:

View Code
 1 <?xml version="1.0" encoding="utf-8"?>  
 2 <Workflow>  
 3   <Activity>  
 4     <ActivityId>1</ActivityId>  
 5     <ActivityName>start</ActivityName>  
 6     <BindingPageId>1</BindingPageId>  
 7     <BindingRoleId>1</BindingRoleId>  
 8     <ActivityLevel>1</ActivityLevel>  
 9   </Activity>  
10   <Activity>  
11     <ActivityId>2</ActivityId>  
12     <ActivityName>pass</ActivityName>  
13     <BindingPageId>2</BindingPageId>  
14     <BindingRoleId>2</BindingRoleId>  
15     <ActivityLevel>2</ActivityLevel>  
16   </Activity>  
17 </Workflow>  

我们需要读取Activity节点下的内容,因为存在多个Activity,所以需要读取多个节点。可以使用 XmlDocument.SelectSingleNode和XmlDocument.SelectNodes方法查找节点。前一个是查找匹配的第一个节点,而后一个则会返回一个节点列表。

1.XmlDocument.SelectSingleNode方法的使用

首先创建一个读取xml文件的方法ReadXmlNode

View Code
 1 01./// <summary>    
 2 02.      /// 读取xml中的指定节点的值  
 3 03.      /// </summary>    
 4 04.      public void ReadXmlNode(string filename)  
 5 05.      {  
 6 06.          XmlDocument xmlDoc = new XmlDocument();  
 7 07.          try  
 8 08.          {  
 9 09.              xmlDoc.Load(filename);  
10 10.              //读取Activity节点下的数据。SelectSingleNode匹配第一个Activity节点  
11 11.              XmlNode root = xmlDoc.SelectSingleNode("//Activity");//当节点Workflow带有属性是,使用SelectSingleNode无法读取          
12 12.              if (root != null)  
13 13.              {  
14 14.                  string ActivityId = (root.SelectSingleNode("ActivityId")).InnerText;  
15 15.                  string ActivityName = (root.SelectSingleNode("ActivityName ")).InnerText;  
16 16.                  string ActivityLevel = root.SelectSingleNode("ActivityLevel").InnerText;  
17 17.                  Console.WriteLine("ActivityId:" + ActivityId + "/nActivityName:" + ActivityName + "/nActivityLevel:" + ActivityLevel);  
18 18.              }  
19 19.              else  
20 20.              {  
21 21.                  Console.WriteLine("the node  is not existed");  
22 22.                  //Console.Read();  
23 23.              }  
24 24.          }  
25 25.          catch (Exception e)  
26 26.          {  
27 27.              //显示错误信息  
28 28.              Console.WriteLine(e.Message);  
29 29.          }  
30 30.      }  

然后在主函数中调用该方法

View Code
1 01.class Program  
2 02.   {  
3 03.       static void Main(string[] args)  
4 04.       {  
5 05.           XMLOperation xmlOpr = new XMLOperation();  
6 06.           xmlOpr.ReadXmlNodes("activity.xml");  
7 07.           Console.Read();  
8 08.       }  
9 09.   }  

输出结果为:

共有2个节点 ActivityId:  1 ActivityName:  start ActivityLevel:  1 ActivityId:  2 ActivityName:  pass ActivityLevel:  2

3.通过节点属性查找指定节点

参考http://www.csharp-examples.net/xml-nodes-by-attribute-value/,虽然没有用到参考文章中的方法,不过总觉得以后会用到的。

 

View Code
 1 01./// <summary>    
 2 02.     ///通过ActivityLevel获取xmlnode,需要保证ActivityLevel在一个xml文档中是唯一的。  
 3 03.     /// </summary>    
 4 04.     public XmlNode getXmlNode(string filename, string activitylevel)  
 5 05.     {  
 6 06.         XmlDocument xmlDoc = new XmlDocument();  
 7 07.         try  
 8 08.         {  
 9 09.             xmlDoc.Load(filename);  
10 10.             XmlNodeList xnList = xmlDoc.SelectNodes("//Activity");//当节点Workflow带有属性是,使用SelectSingleNode无法读取          
11 11.             foreach (XmlNode xn in xnList)  
12 12.             {  
13 13.                 string ActivityLevel = xn.SelectSingleNode("ActivityLevel").InnerText;  
14 14.                 if(activitylevel==ActivityLevel)  
15 15.                 {  
16 16.                     return xn;  
17 17.                 }  
18 18.             }  
19 19.         }  
20 20.         catch (Exception e)  
21 21.         {  
22 22.             //显示错误信息  
23 23.             Console.WriteLine(e.Message);  
24 24.         }  
25 25.         return null;  
26 26.     }  

然后在主函数中调用该方法

View Code
 1 01.class Program  
 2 02.   {  
 3 03.       static void Main(string[] args)  
 4 04.       {  
 5 05.           XMLOperation xmlOpr = new XMLOperation();  
 6 06.           XmlNode xn=xmlOpr.getXmlNode("activity.xml", "2");  
 7 07.           string ActivityId = (xn.SelectSingleNode("ActivityId")).InnerText;  
 8 08.           string ActivityName = (xn.SelectSingleNode("ActivityName ")).InnerText;  
 9 09.           string ActivityLevel = xn.SelectSingleNode("ActivityLevel").InnerText;  
10 10.           Console.WriteLine("ActivityId:" + ActivityId + "/nActivityName:" + ActivityName + "/nActivityLevel:" + ActivityLevel);  
11 11.           Console.Read();  
12 12.       }  
13 13.   }  

输出结果为:

ActivityId:2 ActivityName:pass ActivityLevel:2

 

说明:本文出处:http://blog.csdn.net/xw13106209/article/details/6384218

posted @ 2013-03-07 14:49  银河系上的地球  阅读(858)  评论(0编辑  收藏  举报