繁体   English   中英

如何限制asp.net中XML记录中显示的字符数?

[英]How do I limit the number of characters shown in an XML record in asp.net?

我有一个XML源,其中一个字段是“description”,它的长度可能不同但总是很长。 当我将它传递给我的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();
}

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

我假设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>

注册Repeater的OnItemDataBound事件并编写以下代码。

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) + "...";
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM