Asp.net XML心得2:读xml文档的属性

xml:

 

01.xml
 1 <?xml version="1.0" encoding="utf-8" ?>
 2 <Menus>
 3   <Menu title="常用网址">
 4     <item name="天下网" url="http://www.netskycn.com" id="1"/>
 5     <item name="天下网生活论坛" url="http://life.netskycn.com" id="2"/>
 6     <item name="csdn" url="http://www.csdn.net" id="3"/>
 7     <item name="我的博客" url="http://blog.csdn.net/zhoufoxcn" id="4"/>
 8     <item name="百度" url="http://www.baidu.com" id="5"/>
 9     <item name="Google" url="http://www.google.cn" id="6"/>
10     <item name="微软" url="http://www.microsoft.com" id="7"/>
11   </Menu>
12   <Menu title="娱乐网址">
13     <item name="奇虎" url="http://www.qihoo.com" id="12"/>
14     <item name="网易" url="http://www.163.com" id="13"/>
15     <item name="天涯" url="http://www.tianya.cn" id="14"/>
16   </Menu>
17   <Menu title="安全网址">
18     <item name="360" url="http://www.safe360.com" id="15"/>
19     <item name="瑞星" url="http://www.rising.com.cn" id="16"/>
20   </Menu>
21 </Menus>

 

 

c#:

 

代码
 1 protected void Page_Load(object sender, EventArgs e)
 2     {
 3         using(XmlReader dr = XmlReader.Create(Server.MapPath("01.xml")))
 4         {
 5             while(dr.Read())
 6             {
 7                 if (dr.NodeType == XmlNodeType.Element)
 8                 {
 9                     for (int dep = 0; dep < dr.Depth; dep++)
10                     {
11                         Label1.Text += "===";
12                     }
13                     Label1.Text += ("==>" + dr.Name);
14 
15                     if (dr.HasAttributes)
16                     {
17                         Label1.Text += "(";
18                         for (int count = 0; count < dr.AttributeCount; count++)
19                         {
20                             dr.MoveToAttribute(count);
21                             Label1.Text += dr.Name + "  ";
22                         }
23                         Label1.Text += ")";
24                     }
25                 }
26                 Label1.Text += "</br>";
27             }
28         }
29         
30     }

 

 

 

输出结果:

posted @ 2010-03-24 16:13  HelloCG  阅读(290)  评论(0编辑  收藏  举报