我有一个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();
}
答案 0 :(得分:3)
也许你可以在返回的XPath查询的值上使用SubString吗?
答案 1 :(得分: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>
来自 here
的参考资料将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) + "...";
}
}