使用XmlDocument中的XmlElement遍历xml文档

xml文件各个层次的节点名称都一样,所以不能使用DataSet.ReadXml来加载,必须使用XmlDocument对象:
代码
<? xml version='1.0' encoding='UTF-8' ?>
 
< Value >
   
< Value  nodename ="北京市"  nodeid ="110000" >
     
< Value  nodename ="市辖区"  nodeid ="110100" >
       
< Value  nodename ="东城区"  nodeid ="110101"   />
       
< Value  nodename ="怀柔区"  nodeid ="110116"   />
       
< Value  nodename ="平谷区"  nodeid ="110117"   />
    
</ Value >
     
< Value  nodename ="县"  nodeid ="110200" >
       
< Value  nodename ="密云县"  nodeid ="110228"   />
       
< Value  nodename ="延庆县"  nodeid ="110229"   />
    
</ Value >
  
</ Value >
   
< Value  nodename ="天津市"  nodeid ="120000" >
     
< Value  nodename ="市辖区"  nodeid ="120100" >
       
< Value  nodename ="和平区"  nodeid ="120101"   />
       
< Value  nodename ="河东区"  nodeid ="120102"   />
       
< Value  nodename ="河西区"  nodeid ="120103"   />
       
< Value  nodename ="宝坻区"  nodeid ="120115"   />
     
</ Value >
     
< Value  nodename ="县"  nodeid ="120200" >
       
< Value  nodename ="宁河县"  nodeid ="120221"   />
       
< Value  nodename ="静海县"  nodeid ="120223"   />
       
< Value  nodename ="蓟 县"  nodeid ="120225"   />
    
</ Value >
  
</ Value >
   
< Value  nodename ="河北省"  nodeid ="130000" >
     
< Value  nodename ="石家庄市"  nodeid ="130100" >
       
< Value  nodename ="市辖区"  nodeid ="130101"   />
       
< Value  nodename ="新华区"  nodeid ="130105"   />
     
</ Value >
  
</ Value >
</ Value >

 

 

aspx.cs,转换成符合逻辑层次标准的xml文档

代码
         string  str  =   " <?xml version=\ " 1.0 \ "  encoding=\ " utf - 8 \ " ?> " ;

        str 
+=   " <item> " ;

        XmlDocument xmldc 
=   new  XmlDocument();
        xmldc.Load(Server.MapPath(
" XMLFile.xml " ));

        XmlElement el 
=  xmldc.DocumentElement;

        
for  ( int  i  =   0 ; i  <  el.ChildNodes.Count; i ++ )
        {
            XmlNode nodeProvince 
=  el.ChildNodes[i];
            
string  province  =  nodeProvince.Attributes[ " nodename " ].Value;
            
string  provinceID  =  nodeProvince.Attributes[ " nodeid " ].Value;

            str 
+=   " <province name=\ ""  + province +  " \ "  id=\ ""  + provinceID +  " \ " > " ;

            
for  ( int  j  =   0 ; j  <  el.ChildNodes[i].ChildNodes.Count; j ++ )
            {
                XmlNode nodeCity 
=  el.ChildNodes[i].ChildNodes[j];
                
string  city  =  nodeCity.Attributes[ " nodename " ].Value;
                
string  cityID  =  nodeCity.Attributes[ " nodeid " ].Value;

                str 
+=   " <city name=\ ""  + city +  " \ "  id=\ ""  + cityID +  " \ "  provinceID=\ ""  + provinceID +  " \ " > " ;

                
for  ( int  k  =   0 ; k  <  el.ChildNodes[i].ChildNodes[j].ChildNodes.Count; k ++ )
                {
                    XmlNode nodedistrinct 
=  el.ChildNodes[i].ChildNodes[j].ChildNodes[k];
                    
string  distrinct  =  nodedistrinct.Attributes[ " nodename " ].Value;
                    
string  distrinctID  =  nodedistrinct.Attributes[ " nodeid " ].Value;

                    str 
+=   " <distrinct name=\ ""  + distrinct +  " \ "  distrinctID=\ ""  + distrinctID +  " \ "  cityID=\ ""  + cityID +  " \ "  provinceID=\ ""  + provinceID +  " \ "  /> " ;

                }

                str 
+=   " </city> " ;
            }

            str 
+=   " </province> " ;
        }


        str 
+=   " </item> " ;

        Response.Write(str);

 

 

你可能感兴趣的:(document)