大家好,我想从 asp.net 中的 xml 文件中获取前 5 条记录。请告诉我我该怎么做,我正在从 xml 中获取这样的数据
这是我的转发器标记我正在将我的数据与转发器中的 eval 标记绑定
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<!-- content -->
<div class="post">
<div class="right">
<h2><a href="#">
<asp:Label ID="lbltitle" runat="server" Text='<%#Eval("title ") %>'></asp:Label></a></h2>
<asp:Label ID="lblcontent" runat="server" Text='<%#Eval("Discription") %>'></asp:Label>
</div>
<div class="left">
<p class="dateinfo">
<asp:Label ID="lbldate"
runat="server" Text='<%#Eval("DT") %>'></asp:Label>
<span><asp:Label ID="lblmnth" runat="server" Text='<%#Eval("mnt") %>'></asp:Label></span></p>
<div class="post-meta">
<h4>Post Info</h4>
<ul>
<li class="user"><a href="#">Erwin</a></li>
<li class="time"><a href="#">12:30 PM</a></li>
<li class="comment">
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/PostComment.aspx" >Comments</asp:HyperLink></li>
<li class="permalink"><a href="#">Permalink</a></li>
</ul>
</div>
</div>
</div>
</ItemTemplate>
</asp:Repeater>
C# 代码
var doc = XDocument.Load(Server.MapPath("~/Data/BlogContent.xml"));
var result = doc.Descendants("post").Where(x => x.Element("id") != null).Select(x => new
{
id = x.Element("id").Value,
title = x.Element("title").Value,
Description = x.Element("Discription").Value,
dt = x.Element("dt").Value,
mnt = x.Element("mnt").Value,
yr = x.Element("yr").Value
}).OrderByDescending(x => x.id).Take(5);
Repeater1.DataSource = result;
Repeater1.DataBind();