C# 从 XmlDataSource 填充 DropDownList
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/631365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Populate DropDownList from XmlDataSource
提问by Luca Martinetti
I'd like to populate my DropDownList using a simple xml file:
我想使用一个简单的 xml 文件填充我的 DropDownList:
<?xml version="1.0" encoding="utf-8" ?>
<Databases>
<Database>foo</Database>
<Database>bar</Database>
<Database>baz</Database>
</Databases>
My XPath is
我的 XPath 是
/Databases/Database
My drop down list is rendered as:
我的下拉列表呈现为:
<select name="databaseDropDownList" id="databaseDropDownList">
<option selected="selected" value="System.Web.UI.WebControls.XmlDataSourceNodeDescriptor">System.Web.UI.WebControls.XmlDataSourceNodeDescriptor</option>
<option value="System.Web.UI.WebControls.XmlDataSourceNodeDescriptor">System.Web.UI.WebControls.XmlDataSourceNodeDescriptor</option>
<option value="System.Web.UI.WebControls.XmlDataSourceNodeDescriptor">System.Web.UI.WebControls.XmlDataSourceNodeDescriptor</option>
</select>
How should I extract the text?
我应该如何提取文本?
Thanks
谢谢
采纳答案by Darin Dimitrov
I can't recall it from the top of my head but I think there was a bug in XmlDataSource that prevents you to bind to values of xml nodes. It works with attributes only. Please correct me if I am wrong with this. There's a slight modification you need to make to your XML file:
我想不起来了,但我认为 XmlDataSource 中有一个错误,它阻止您绑定到 xml 节点的值。它仅适用于属性。如果我错了,请纠正我。您需要对 XML 文件稍作修改:
<%@ Page Language="C#" %>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string xml =
@"<?xml version=""1.0"" encoding=""utf-8"" ?>
<Databases>
<Database name=""foo"" />
<Database name=""bar"" />
<Database name=""baz"" />
</Databases>";
databasesSource.Data = xml;
databasesSource.DataBind();
}
}
</script>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="databases" runat="server" DataSourceID="databasesSource" DataValueField="name" DataTextField="name" />
<asp:XmlDataSource ID="databasesSource" runat="server" XPath="/Databases/Database" />
</div>
</form>
</body>
</html>
Note that I added the nameattribute instead of using the value of the node directly.
请注意,我添加了name属性,而不是直接使用节点的值。
If you can't modify the structure of your original XML file you can apply an XSLT transformation on it using the TransformFileproperty as described in this post.
如果你不能修改原始的XML文件的结构,你可以使用它应用XSLT转换TransformFile财产描述,在此职位。
回答by Andrew Hare
Here is one way of doing it - you can project an array of ListItems
in a LINQ query:
这是一种方法 - 您可以ListItems
在 LINQ 查询中投影一个数组:
XDocument doc = XDocument.Parse(@"<Databases>
<Database>foo</Database>
<Database>bar</Database>
<Database>baz</Database>
</Databases>");
YourList.Items.AddRange(
(from XElement el in doc.Descendants("Database")
select new ListItem(el.Value)).ToArray()
);
回答by Andrew Hare
I had the same problem today. My solution:
我今天遇到了同样的问题。我的解决方案:
This is my xml:
这是我的 xml:
<?xml version="1.0" encoding="utf-8"?>
<pokemons>
<pokemon>
<nome itemname="bulbassaur">bulbassaur </nome>
</pokemon>
<pokemon>
<nome itemname="charmander">chamander </nome>
</pokemon>
<pokemon>
<nome itemname="squirtle"> squirtle </nome>
</pokemon>
</pokemons>
And I put DataTextField="itemname" on the DropDownList server control. ex:
我将 DataTextField="itemname" 放在 DropDownList 服务器控件上。前任:
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
DataSourceID="XmlDataSource1" DataTextField="itemname">
It's working without problems. Probably not the best solution,... but at least better than System.Web.UI.WebControls.XmlDataSourceNodeDescriptor
.
它工作没有问题。可能不是最好的解决方案,但至少比System.Web.UI.WebControls.XmlDataSourceNodeDescriptor
.