<countrys>
  <country id="China" value="China">
    <city id="hb" value="Hubei" /> 
    <city id="gz" value="Guangzhou" />
    <city id="hn" value="Hunan" />
    <city id="jx" value="jiangxi" />
  </country>  <country id="USA" value="USA">
    <city id="ny" value="New Yory" />
    <city id="hsd" value=" Washington " />
  </country>
</countrys>
上面这个xml文档,China 和 USA在下拉列表框中显示,当我选中下拉列表中的值时,在则显示相应的子节点的value值,这里应该怎么获得父节点下的子节点呢
//获得下拉列表选中的值
string id = countries.SelectedValue.ToString();//获得被选中的国家id
XDocument xdoc = XDocument.Load(HttpContext.Current.Server.MapPath("~/App_XMLs/Countrys.xml"));//获取Xml的数据
//获得下拉列表被选中的父节点
var country=from c in xdoc .Descendants ("country") where( c.Attribute ("id").Value ==id )select c ; 
下面怎么获得那些子节点呢?
        
       

解决方案 »

  1.   

     
    //获得下拉列表被选中的父节点
    var country=from c in xdoc .Descendants ("country") where( c.Attribute ("id").Value ==id )select c ; 
    这里获得的父节点是var类型的,点ChildNodes点不出来
      

  2.   

    参见
    DropDownList 绑定 XML 和 DropDownList 绑定 XML 数据并进行联动的例子
    http://dotnet.aspx.cc/file/DataBind-XML-DropDownList-XmlDataSource.aspx
      

  3.   

    XmlNodeList nodes = doc.SelectNodes("countrys/country[@value='" + this.City.SelectedValue + "']/city ");
      

  4.   

    我的资源里面有xml的增删改查小项目,呵呵!飘过~~
      

  5.   

     var sons= xdoc.Descendants("country").Where(c => c.Attribute("id").Value == id).Select(c => c.Descendants("city"));