【问题标题】:Trouble binding XML child to DataList using ReadXML()使用 ReadXML() 将 XML 子项绑定到 DataList 时遇到问题
【发布时间】:2015-08-10 19:37:19
【问题描述】:

我正在尝试将 ASP DataList 控件与从 XML 文件返回的值绑定。有点独特的是我不是从文件中加载 XML,而是使用 XMLTextReader() 将其作为字符串加载。这样做的原因是 XML 将根据从数据库中读取的值构建。下面的代码采用(在本例中为硬编码)XML 字符串,将其添加到 DataSet,然后绑定 DataList 控件...

StringBuilder xml = new StringBuilder();

    xml.Append("<product>");
    xml.Append("<sku>241</sku>");
    xml.Append("<prodID>2SIDED</prodID>");
    xml.Append("<name>Product Name</name>");
    xml.Append("<price>6.99</price>");
    xml.Append("<description>Product Description</description>");
    xml.Append("<standalone>1</standalone>");
    xml.Append("<sellable>1</sellable>");
    xml.Append("<product-type>10</product-type>");
    xml.Append("<customization>");
    xml.Append("<option1></option1>");
    xml.Append("<option2></option2>");
    xml.Append("<option3></option3>");
    xml.Append("</customization>");
    xml.Append("</product>");

using (XmlTextReader tr = new XmlTextReader(new StringReader(xml.ToString())))
    {
        var document = XElement.Load(tr);
        string source = document.ToString();
        DataSet dsProdList = new DataSet();
        dsProdList.ReadXml(new StringReader(source));
        prodDetail.DataSource = dsProdList;
        prodDetail.DataBind();
    }

这是 DataList 控件...

<asp:DataList ID="prodDetail" runat="server" OnItemDataBound="prodDetail_ItemDataBound">
    <ItemTemplate>
        <div class="proddetail">
            <asp:Image ID="Image" runat="server" width="480" height="325" AlternateText='<%#DataBinder.Eval(Container.DataItem, "name") %>'/>
            <asp:HiddenField ID="hdnStandalone" runat="server" Value='<%#DataBinder.Eval(Container.DataItem, "standalone") %>' Visible="false" />
            <asp:HiddenField ID="hdnSellable" runat="server" Value='<%#DataBinder.Eval(Container.DataItem, "sellable") %>' Visible="false" />
            <asp:HiddenField ID="hdnTypeId" runat="server" Value='<%#DataBinder.Eval(Container.DataItem, "product-type") %>' Visible="false" />
            <asp:HiddenField ID="hdnOption1" runat="server" Value='<%# DataBinder.Eval(Container.DataItem, "option1") %>' Visible="false" />
            <asp:HiddenField ID="hdnOption2" runat="server" Value='<%#DataBinder.Eval(Container.DataItem, "option2") %>' Visible="false" />
            <asp:HiddenField ID="hdnOption3" runat="server" Value='<%#DataBinder.Eval(Container.DataItem, "option3") %>' Visible="false" />
            <asp:Label ID="lblDescription" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "description") %>'>
        </div>
    </ItemTemplate>
</asp:DataList>

这似乎工作正常,直到代码尝试绑定“option1”,此时我得到以下异常: DataBinding:“System.Data.DataRowView”不包含名为“option1”的属性。

据我所知,这肯定是“option1”是“customization”的子项的问题。如果我简单地注释掉打开和关闭“自定义”标签,这实际上将“选项”作为“产品”的子代,代码运行良好,我得到了我期待的输出。

有什么方法可以格式化 DataBinder.Eval() 以查看子项?如果是这样,我无法弄清楚,并且数小时的搜索不成功。还是我应该以不同的方式解决这个问题?也许不使用数据集?任何帮助表示赞赏。谢谢。

【问题讨论】:

    标签: c# asp.net xml linq data-binding


    【解决方案1】:

    您可以尝试使用 xDoclinqanonymous 输入 as,

    prodDetail.DataSource = from item in xDoc.Descendants("product") 
            from needThis in item.Descendants("customization")
            select new 
            {       ProductName = item.Element("sku").Value,
                    ID = item.Element("prodID").Value,
                    //----other properties----
                    Option1 = needThis.Element("option1").Value,
                    Option2 = needThis.Element("option2").Value,
                    //---and some more properties----
            };
    

    您必须确保将anonymous 对象的属性名称与您在datalist 中的数据绑定中所期望的属性名称相同。

    【讨论】:

    • 成功了,感谢 Coder of Code!我曾使用 linq 尝试过类似的解决方案,虽然它没有抛出任何错误,但也没有返回任何结果。使用 XDocument 是这里的关键。再次感谢!
    猜你喜欢
    • 2012-09-02
    • 1970-01-01
    • 2017-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-12
    • 2011-04-10
    • 1970-01-01
    相关资源
    最近更新 更多