XmlReader在未知元素的名称和属性的名称的情况下读取属性

经过昨天到今天的努力以及博问上好心人的帮助,终于解决了XmlReader在未知元素的名称和属性的名称的情况下读取属性的方法。

在没有解决前,我的代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Xml;
 6 
 7 namespace ReadAttribute
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             string path = @"e:\testfile\myMail.xml";
14             
15             #region 读取属性的第一种方法
16             //string date;
17             //try
18             //{
19             //    XmlReader xr = XmlReader.Create(path);
20             //    xr.ReadToFollowing("mail");
21             //    date = xr.GetAttribute("date");
22             //    Console.Write("信件的日期为:");
23             //    Console.WriteLine(date);
24             //}
25             //catch (Exception ex)
26             //{
27             //    Console.WriteLine(ex.Message);
28             //} 
29             #endregion
30 
31             #region 读取属性的第二种方法
32             try
33             {
34                 XmlReader xr = XmlReader.Create(path);
35                 while (xr.Read())
36                 {
37                     if (xr.HasAttributes)
38                     {
39                         //Console.WriteLine("<" + xr.Name + ">的属性:");
40                         //for (int i = 0; i < xr.AttributeCount; i++)
41                         //{                            
42                         //xr.MoveToAttribute(i);                            
43                         Console.WriteLine("<" + xr.Name + ">的属性:");
44                         Console.WriteLine("{0}={1}", xr.Name, xr.Value);
45                         //}                                                                  
46                     }
47                 }
48             }
49             catch (Exception ex)
50             {
51                 Console.WriteLine(ex.Message);
52             } 
53             #endregion
54             Console.ReadKey();
55         }
56     }
57 }
View Code

解决后,我的代码如下:

 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 using System.Text;
 5 using System.Xml;
 6 
 7 namespace ReadAttribute
 8 {
 9     class Program
10     {
11         static void Main(string[] args)
12         {
13             string path = @"e:\testfile\myMail.xml";
14             
15             #region 读取属性的第一种方法
16             //string date;
17             //try
18             //{
19             //    XmlReader xr = XmlReader.Create(path);
20             //    xr.ReadToFollowing("mail");
21             //    date = xr.GetAttribute("date");
22             //    Console.Write("信件的日期为:");
23             //    Console.WriteLine(date);
24             //}
25             //catch (Exception ex)
26             //{
27             //    Console.WriteLine(ex.Message);
28             //} 
29             #endregion
30 
31             #region 读取属性的第二种方法
32             try
33             {
34                 XmlReader xr = XmlReader.Create(path);
35                 while (xr.Read())
36                 {
37                     if (xr.HasAttributes)
38                     {
39                         Console.WriteLine("<" + xr.Name + ">的属性:");
40                         for (int i = 0; i < xr.AttributeCount; i++)
41                         {                            
42                         xr.MoveToAttribute(i);                            
43                         //Console.WriteLine("<" + xr.Name + ">的属性:");
44                         Console.WriteLine("{0}={1}", xr.Name, xr.Value);
45                         }                                                                  
46                     }
47                 }
48             }
49             catch (Exception ex)
50             {
51                 Console.WriteLine(ex.Message);
52             } 
53             #endregion
54             Console.ReadKey();
55         }
56     }
57 }
View Code

解决后,上面的代码可以不用知道元素的名称和属性的名称的情况下读取XML文件中的所有属性名及其值。这种方法有点像迭代器遍历数组,只不过这里的元素变成了XML的节点和元素,而且这里可以把XML的属性也可以看成是XML的节点(”可以看成XML元素的子节点“),这样元素看成是节点,属性也看成是节点,都当做节点处理,也就是说这个遍历可以看成是遍历XML的节点。这样也就可以解释上面代码中的 xr.Name、xr.Value了,他们分别可以看成是XML的节点名和节点值(即属性名和属性值)。如此,这样的方法便不难理解了!

posted @ 2014-08-10 12:32  Jack Leonardo  阅读(587)  评论(0编辑  收藏  举报