日期:2014-05-20 浏览次数:20959 次
<Provinces> <Province ID="1" Name="北京市">北京市</Province> <Province ID="2" Name="天津市">天津市</Province> </Provinces>
XElement x = XElement.Load("D:\\ajax+jquery联动下拉\\ajax+jquery联动下拉\\ajax+jquery联动下拉\\XML\\Province.xml"); ddlCountry.DataSource= from pn in x.Descendants("Province") select x; ddlCountry.DataTextField = "Name"; ddlCountry.DataValueField = "ID"; ddlCountry.DataBind();
ddlCountry.DataSource= from pn in x.Descendants("Province") select new{ Name=x.Attribute("Name").Value, ID=x.Attribute("ID").Value } ddlCountry.DataTextField = "Name"; ddlCountry.DataValueField = "ID"; ddlCountry.DataBind();
------解决方案--------------------
ddlCountry.DataSource= from pn in x.Descendants("Province")
select new{
Name=x.Attribute("Name")==null?"":x.Attribute("Name").Value,
ID=x.Attribute("ID")==null?"":x.Attribute("ID").Value
}
ddlCountry.DataTextField = "Name";
ddlCountry.DataValueField = "ID";
ddlCountry.DataBind();