数据绑定 xml 列表到 asp.net dropdownlist 的问题
我有这个 xml 文件,现在我想向我的 xml 文件添加新条目
之前:
<?xml version="1.0" encoding="utf-8"?>
<email>
<builderemail>
<builder>
<id>1</id>
<value>[email protected]</value>
</builder>
</builderemail>
<manageremail>
<manager>
<id>1</id>
<value>[email protected]</value>
</manager>
</manageremail>
</email>
之后:
<?xml version="1.0" encoding="utf-8"?>
<email>
<builderemail>
<builder>
<id>1</id>
<value>[email protected]</value>
<id>2</id>
<value>Others</value>
</builder>
</builderemail>
<manageremail>
<manager>
<id>1</id>
<value>[email protected]</value>
<id>2</id>
<value>Others</value>
</manager>
</manageremail>
</email>
我的代码 c# 代码:
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Visible = false;
Button1.Visible = false;
TextBox2.Visible = false;
Button2.Visible = false;
if (!IsPostBack)
{
PopulateDDLFromXMLFile();
}
}
public void PopulateDDLFromXMLFile()
{
DataSet ds = new DataSet();
ds.ReadXml(MapPath("~/App_Data/builderemail.xml"));
//get the dataview of table "Country", which is default table name
DataView dv = ds.Tables["builder"].DefaultView;
DataView dw = ds.Tables["manager"].DefaultView;
//or we can use:
//DataView dv = ds.Tables[0].DefaultView;
//Now sort the DataView vy column name "Name"
dv.Sort = "value";
//now define datatext field and datavalue field of dropdownlist
DropDownList1.DataTextField = "value";
DropDownList1.DataValueField = "ID";
DropDownList2.DataTextField = "value";
DropDownList2.DataValueField = "ID";
//now bind the dropdownlist to the dataview
DropDownList1.DataSource = dv;
DropDownList1.DataBind();
DropDownList2.DataSource = dw;
DropDownList2.DataBind();
}
但是,这不起作用。我将收到 IndexOutOfRangeException 错误。我该如何处理这个问题?
I have this xml file, and now i will want to add new entries to my xml file
Before:
<?xml version="1.0" encoding="utf-8"?>
<email>
<builderemail>
<builder>
<id>1</id>
<value>[email protected]</value>
</builder>
</builderemail>
<manageremail>
<manager>
<id>1</id>
<value>[email protected]</value>
</manager>
</manageremail>
</email>
After:
<?xml version="1.0" encoding="utf-8"?>
<email>
<builderemail>
<builder>
<id>1</id>
<value>[email protected]</value>
<id>2</id>
<value>Others</value>
</builder>
</builderemail>
<manageremail>
<manager>
<id>1</id>
<value>[email protected]</value>
<id>2</id>
<value>Others</value>
</manager>
</manageremail>
</email>
my code c# codes:
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Visible = false;
Button1.Visible = false;
TextBox2.Visible = false;
Button2.Visible = false;
if (!IsPostBack)
{
PopulateDDLFromXMLFile();
}
}
public void PopulateDDLFromXMLFile()
{
DataSet ds = new DataSet();
ds.ReadXml(MapPath("~/App_Data/builderemail.xml"));
//get the dataview of table "Country", which is default table name
DataView dv = ds.Tables["builder"].DefaultView;
DataView dw = ds.Tables["manager"].DefaultView;
//or we can use:
//DataView dv = ds.Tables[0].DefaultView;
//Now sort the DataView vy column name "Name"
dv.Sort = "value";
//now define datatext field and datavalue field of dropdownlist
DropDownList1.DataTextField = "value";
DropDownList1.DataValueField = "ID";
DropDownList2.DataTextField = "value";
DropDownList2.DataValueField = "ID";
//now bind the dropdownlist to the dataview
DropDownList1.DataSource = dv;
DropDownList1.DataBind();
DropDownList2.DataSource = dw;
DropDownList2.DataBind();
}
However, this does not work. I will get a IndexOutOfRangeException error. how do i handle this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
“IndexOutOfRange”异常是由于这行代码造成的:
方法 PopulateDDLFromXMLFile 适用于 xml 的第一个版本。
最新版本的 xml 不是有效的版本,并且不代表您想要/尝试在 PopulateDDLFromXMLFile 方法中执行的操作。
在较新的 xml 上调用上述代码会创建 6 个表,其中一个用于 builderemail、builder、id、value、manageremail 和 manager。
但代码假设 id 和 value 是构建器表内的列。
因此出现了上述例外。
我建议你将xml修改为:
The "IndexOutOfRange" exception is because of this line of code:
The method PopulateDDLFromXMLFile works with the first version of xml.
The later version of xml is not a valid one and doesn't represent what you want to / trying to do in PopulateDDLFromXMLFile method.
Calling above code on the newer xml creates 6 tables, one for builderemail, builder, id, value, manageremail and manager.
But the code assumes that id and value would be columns inside builder table.
Hence the above exception.
I would suggest you modify the xml as: