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

2014/08/10 posted in  dotNET

经过努力,终于解决了XmlReader在未知元素的名称和属性的名称的情况下读取属性的方法。

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

	using System; 
	using System.Collections.Generic; 
	using System.Linq; 
	using System.Text; 
	using System.Xml; 
	
	namespace ReadAttribute 
	{ 
	    class Program
	     {
	         static void Main(string[] args)
	         {
	             string path = @"e:\testfile\myMail.xml";
	             
	             #region 读取属性的第一种方法
	             //string date;
	             //try
	             //{
	             //    XmlReader xr = XmlReader.Create(path);
	             //    xr.ReadToFollowing("mail");
	             //    date = xr.GetAttribute("date");
	             //    Console.Write("信件的日期为:");
	             //    Console.WriteLine(date);
	             //}
	             //catch (Exception ex)
	             //{
	             //    Console.WriteLine(ex.Message);
	             //} 
	             #endregion
	 
	             #region 读取属性的第二种方法
	             try
	             {
	                 XmlReader xr = XmlReader.Create(path);
	                 while (xr.Read())
	                 {
	                     if (xr.HasAttributes)
	                     {
	                         //Console.WriteLine("<" + xr.Name + ">的属性:");
	                         //for (int i = 0; i < xr.AttributeCount; i++)
	                         //{                            
	                         //xr.MoveToAttribute(i);                            
	                         Console.WriteLine("<" + xr.Name + ">的属性:");
	                         Console.WriteLine("{0}={1}", xr.Name, xr.Value);
	                         //}                                                                  
	                     }
	                 }
	             }
	             catch (Exception ex)
	             {
	                 Console.WriteLine(ex.Message);
	             } 
	             #endregion
	             Console.ReadKey();
	         }
	     }
	 }

解决后,我的代码如下:

	using System; 
	using System.Collections.Generic; 
	using System.Linq; 
	using System.Text; 
	using System.Xml; 
	
 	namespace ReadAttribute 
	{ 
	    class Program
	     {
	         static void Main(string[] args)
	         {
	             string path = @"e:\testfile\myMail.xml";
	             
	             #region 读取属性的第一种方法
	             //string date;
	             //try
	             //{
	             //    XmlReader xr = XmlReader.Create(path);
	             //    xr.ReadToFollowing("mail");
	             //    date = xr.GetAttribute("date");
	             //    Console.Write("信件的日期为:");
	             //    Console.WriteLine(date);
	             //}
	             //catch (Exception ex)
	             //{
	             //    Console.WriteLine(ex.Message);
	             //} 
	             #endregion
	 
	             #region 读取属性的第二种方法
	             try
	             {
	                 XmlReader xr = XmlReader.Create(path);
	                 while (xr.Read())
	                 {
	                     if (xr.HasAttributes)
	                     {
	                         Console.WriteLine("<" + xr.Name + ">的属性:");
	                         for (int i = 0; i < xr.AttributeCount; i++)
	                         {                            
	                         xr.MoveToAttribute(i);                              
	                         
	                         //Console.WriteLine("<" + xr.Name + ">的属性:");
	                         Console.WriteLine("{0}={1}", xr.Name, xr.Value);
	                         }                                                                     								}
	                }
	             }
	             catch (Exception ex)
	             {
	                 Console.WriteLine(ex.Message);
	             } 
	             #endregion
	             Console.ReadKey();
	         }
	     }
	 }

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