重复项目将 XML 加载到复选框列表 (vb.net)

发布于 2024-12-26 09:50:55 字数 992 浏览 3 评论 0原文

您好,我正在尝试加载 XML 文档并填充一些复选框列表。我使用 DataTextField 如下。

    <asp:CheckBoxList ID="cblcountry" runat="server" DataTextField="CountryofBirth">

在后面的代码中我使用下面的代码。

    Dim dSet As New DataSet
    dSet.ReadXml(Server.MapPath("xsource.xml"))
    cblcountry.DataSource = dSet
    cblcountry.DataBind()

这是 xml 文档。

    <pupil>
    <academicYear>2011/2010</academicYear>
    <grade>Kindergarten 1</grade>
    <class>class 1</class>    
    <name>emma</name>
    <admissionDate>01/05/2010</admissionDate>
    <CountryofBirth>United Kingdom</CountryofBirth>
    <fullName>emma watson</fullName>
    </pupil>

它加载一切都很好。但它有重复的项目。例如,如果有 5 个人的出生国家/地区是英国,则复选框列表会显示 5 次。如果我从 1 个 XML 块中删除 CountryofBirth,它仍然显示一个空白复选框项。

所以我的问题是如何获取复选框列表而不显示那些重复的数据。我正在开发一个原型(工作演示),因此最简单的方法将更受赞赏。

非常感谢你们。我喜欢这个论坛。

Hi i am trying to load XML document and populate few checkboxlist. I use DataTextField as below.

    <asp:CheckBoxList ID="cblcountry" runat="server" DataTextField="CountryofBirth">

And in the code behind i use this code below.

    Dim dSet As New DataSet
    dSet.ReadXml(Server.MapPath("xsource.xml"))
    cblcountry.DataSource = dSet
    cblcountry.DataBind()

And this is xml doc.

    <pupil>
    <academicYear>2011/2010</academicYear>
    <grade>Kindergarten 1</grade>
    <class>class 1</class>    
    <name>emma</name>
    <admissionDate>01/05/2010</admissionDate>
    <CountryofBirth>United Kingdom</CountryofBirth>
    <fullName>emma watson</fullName>
    </pupil>

It loads everything fine. But it has duplicated items. For example, if there are 5 people with CountryofBirth is United Kingdom than checkboxlist show it 5 times. If i delete CountryofBirth from 1 block of XML, it still shows a blank checkboxitem.

So my question is how do i get checkboxlist without showing those duplicated data. I am just developing a prototype (working demo) so easiest way would be more appreciated.

Thanks so much guys. I love this forum.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

自由如风 2025-01-02 09:50:56

下拉列表将为 XML 文档中的每个 Pupil 元素添加一个列表项。您应该首先将 XML(或数据集)过滤到不同的国家/地区列表,然后绑定到下拉列表控件。

使用 Linq 示例进行编辑:

首先,从 Aspx 页面中删除 DataTextField="CountryofBirth"。

第二...

    Dim dSet As New DataSet
    dSet.ReadXml(Server.MapPath("xsource.xml"))

    Dim lstCountries As List(Of String) = (From dr As DataRow In ds.Tables(0).Rows _
                                           Select CStr(dr.Item("CountryofBirth"))).Distinct.ToList()

    cblcountry.DataSource = lstCountries
    cblcountry.DataBind()

The drop down list will add a list item for every Puplil element in the XML document. You should first filter the XML (or dataset) to a distinct list of countries and then bind to the drop down list control.

Edit w/ Linq Example:

First, remove the DataTextField="CountryofBirth" from your Aspx Page.

Second...

    Dim dSet As New DataSet
    dSet.ReadXml(Server.MapPath("xsource.xml"))

    Dim lstCountries As List(Of String) = (From dr As DataRow In ds.Tables(0).Rows _
                                           Select CStr(dr.Item("CountryofBirth"))).Distinct.ToList()

    cblcountry.DataSource = lstCountries
    cblcountry.DataBind()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文