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 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 }