Asp.net XML心得1:读xml

xml:01.xml

 

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

 

 

c#:

代码
     protected   void  Page_Load( object  sender, EventArgs e)
    {
        
using  (XmlReader dr  =  XmlReader.Create(Server.MapPath( " 01.xml " )))
        {
            Label1.Text 
=   " <ul> " ;
            
while  (dr.Read())
            {
                
if  (dr.NodeType  ==  XmlNodeType.Element)
                {                   
                    Label1.Text 
+=   " <li> " ;
                    
for  ( int  count  =   0 ; count  <  dr.Depth; count ++ )
                    {
                        Label1.Text 
+=   " ........ " ;
                    }
                    
                    
for  ( int  j  =   0 ; j  <  dr.AttributeCount; j ++ )
                    {
                        dr.MoveToAttribute(j);
                        Label1.Text 
+=  (dr.Value  +   "  &nbsp;  " );
                    }
                    Label1.Text 
+=   " </li> " ;                 
                }
            } 
            Label1.Text 
+=   " </ul> " ;
        }
    }
    
protected   void  Button1_Click( object  sender, EventArgs e)
    {
        DataSet ds 
=   new  DataSet();
        ds.ReadXml(Server.MapPath(
" 01.xml " ));
        GridView1.DataSource 
=  ds.Tables[ 1 ].DefaultView;
        GridView1.DataBind();
    }

 

 

输出结果:

Asp.net XML心得1:读xml

你可能感兴趣的:(asp.net)