[英]How I can bind a XDocument obect to DropDownList?
嗨,我想將我的XDocument對象包含在ASP.NET的DropDownList中。
我的ASPX:
<asp:DropDownList ID="drpLogLocation" runat="server" AutoPostBack=true onselectedindexchanged="drpLogLocation_SelectedIndexChanged">
我的C#代碼:
XDocument x = XDocument.Load(Server.MapPath(@"~\App_Data\location.xml"));
x.Root.Descendants()
.Where(e => !ActiveUserList.Contains((string)e.Attribute("group")))
.ToList()
.ForEach(s => s.Remove());
drpLogLocation.DataSource = x;// ?????????????
drpLogLocation.DataBind();
這是我的XML結構:
<plants>
<plant id="DB" display="Dill" group="NPS_DB" />
<plant id="SB" display="Süd" group="NPS_SB" />
</plants>
我想要我的DropDownList DataTextField =“display”和DataValueField =“id”。 我怎么能這樣做
XDocument xDoc = XDocument.Load(@"Yourxmlfile.xml");
var query = from xEle in xDoc.Descendants("publication")
select new ListItem(xEle.Element("name").Value, xEle.Attribute("tcmid").Value);
ddlList.DataValueField = "value";
ddlList.DataTextField = "text";
ddlList.DataSource = query;
ddlList.DataBind();
* 使用Linq而不是更好的解決方案*
您可以從XMLDocument獲取DataSet並像這樣設置下拉列表
string xml = @"<plants> <plant id='DB' display='Dill' group='NPS_DB' /> <plant id='SB' display='Süd' group='NPS_SB' /></plants>";
DataSet ds = new DataSet();
ds.ReadXml(XmlReader.Create(new StringReader(xml)));
ddlList.DataValueField = "DB";
ddlList.DataTextField = "Dill";
ddlList.DataSource = ds.Tables[0];
ddlList.DataBind();
要么
XmlDataDocument doc = new XmlDataDocument();
doc.LoadXml(@"Yourxmlfile.xml");
DataSet ds = doc.DataSet;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.