数据绑定 xml 列表到 asp.net dropdownlist 的问题

发布于 2024-10-16 07:22:45 字数 2754 浏览 1 评论 0原文

我有这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

南笙 2024-10-23 07:22:45

“IndexOutOfRange”异常是由于这行代码造成的:

dv.Sort = "value"; //because the Table "builder" doesn't have the "value" column

方法 PopulateDDLFromXMLFile 适用于 xml 的第一个版本。
最新版本的 xml 不是有效的版本,并且不代表您想要/尝试在 PopulateDDLFromXMLFile 方法中执行的操作。

DataSet ds = new DataSet();
ds.ReadXml(MapPath("~/App_Data/builderemail.xml"));

在较新的 xml 上调用上述代码会创建 6 个表,其中一个用于 builderemail、builder、id、value、manageremail 和 manager。

但代码假设 id 和 value 是构建器表内的列。
因此出现了上述例外。

我建议你将xml修改为:

<?xml version="1.0" encoding="utf-8" ?>
<email>
    <builderemail>
        <builder>
            <id>1</id>
            <value>[email protected]</value>
        </builder>
        <builder>
            <id>2</id>
            <value>Others</value>
            </builder>
    </builderemail>
    <manageremail>
        <manager>
            <id>1</id>
            <value>[email protected]</value>
        </manager>
            <manger>
                <id>2</id>
                <value>Others</value>
            </manger>
    </manageremail>
</email>

The "IndexOutOfRange" exception is because of this line of code:

dv.Sort = "value"; //because the Table "builder" doesn't have the "value" column

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.

DataSet ds = new DataSet();
ds.ReadXml(MapPath("~/App_Data/builderemail.xml"));

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:

<?xml version="1.0" encoding="utf-8" ?>
<email>
    <builderemail>
        <builder>
            <id>1</id>
            <value>[email protected]</value>
        </builder>
        <builder>
            <id>2</id>
            <value>Others</value>
            </builder>
    </builderemail>
    <manageremail>
        <manager>
            <id>1</id>
            <value>[email protected]</value>
        </manager>
            <manger>
                <id>2</id>
                <value>Others</value>
            </manger>
    </manageremail>
</email>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文