1

我有一个 XML 源,其​​中一个字段是“描述”,它的长度可能会有所不同,但总是相当长。当我将它传递给我的 asp.net 中继器时,为了保持一致性和简洁性,我想限制显示的字符数。有没有办法做到这一点?说... 300 个字符。

先感谢您!

我的前端代码:

       <asp:Repeater ID="xPathRepeater" runat="server">
        <ItemTemplate>
            <li>
                <h3><%#XPath ("title") %></h3>
                <p><%#XPath("description")%></p>
            </li>
        </ItemTemplate>
       </asp:Repeater>

我背后的代码:

    protected void XMLsource()
{
    string URLString = "http://ExternalSite.com/xmlfeed.asp";

    XmlDataSource x = new XmlDataSource();
    x.DataFile = URLString;
    x.XPath = String.Format(@"root/job [position() < 5]");

    xPathRepeater.DataSource = x;
    xPathRepeater.DataBind();
}
4

2 回答 2

3

也许您可以在返回的 XPath 查询的值上使用 SubString?

于 2012-03-26T18:02:59.007 回答
1

我假设 XML 可以如下所示。

<Root>
   <Row id="1">
     <title>contact name 1</name>
     <desc>contact note 1</note>
   </Row>
   <Row id="2">
     <title>contact name 2</title>
     <desc>contact note 2</desc>
   </Row>
</Root>

这里参考

将您的 HTML 替换为以下内容。

<h3><asp:Label ID="title" runat="server"></asp:Label></h3>
<p><asp:Label ID="desc" runat="server"></asp:Label></p>

注册OnItemDataBoundRepeater的事件并编写以下代码..

protected void ED_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item)
    {
        Label title = (Label)e.Item.FindControl("title");
        title.Text = ((System.Xml.XmlElement)e.Item.DataItem).ChildNodes[0].InnerText;

        Label desc = (Label)e.Item.FindControl("desc");
        desc.Text = ((System.Xml.XmlElement)e.Item.DataItem).ChildNodes[1].InnerText.Substring(1, 300) + "...";
    }
}
于 2012-03-26T19:13:02.430 回答