adamullah
asked on
Reading Top Five records from XML file ......
Let say I have 100 articles in my XML file and I like to read the top most five articles, how would I do this?
I can read all the articles like this
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath( "Articles. xml"));
DataView dv = ds.Tables[0].DefaultView;
DataList1.DataSource = dv;
DataList1.DataBind();
But I don’t know how would I read top five articles from the xml file.
Thanks
XmlFile
--------------
<?xml version="1.0"?>
<articles>
<article>
<id>050</id>
<title>Article 050 Title</title>
<author>SomeGuy</author>
<dateposted>10/22/2003</da teposted>
<smalldescription>Small description</smalldescript ion>
<longdescription>Long Description</longdescripti on>
<code>Some code</code>
</article>
</articles>
C# Code
private void Page_Load(object sender, System.EventArgs e)
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath( "Articles. xml"));
DataView dv = ds.Tables[0].DefaultView;
DataList1.DataSource = dv;
DataList1.DataBind();
}
I can read all the articles like this
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath(
DataView dv = ds.Tables[0].DefaultView;
DataList1.DataSource = dv;
DataList1.DataBind();
But I don’t know how would I read top five articles from the xml file.
Thanks
XmlFile
--------------
<?xml version="1.0"?>
<articles>
<article>
<id>050</id>
<title>Article 050 Title</title>
<author>SomeGuy</author>
<dateposted>10/22/2003</da
<smalldescription>Small description</smalldescript
<longdescription>Long Description</longdescripti
<code>Some code</code>
</article>
</articles>
C# Code
private void Page_Load(object sender, System.EventArgs e)
{
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath(
DataView dv = ds.Tables[0].DefaultView;
DataList1.DataSource = dv;
DataList1.DataBind();
}
What about using XPath on the XML first to extract the required subset of article elements - something like
//article[position() <= 5]/*
where 5 is the first five elements
//article[position() <= 5]/*
where 5 is the first five elements
that should work as long as position() is a supported function by MS... I've found that alot of them aren't... you should be able to find that info here: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/xmlsdk/htm/xsd_ref_8nas.asp
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
purpleblob: good work, I will keep this method in mind.
ASKER
Thanks purpleblob, i also find a good site for XPath. http://www.w3schools.com/xpath/xpath_location.asp
Here is the modified code.
C# codebehind code
------------------------
private void Page_Load(object sender, System.EventArgs e)
{
XmlDocument xd = new XmlDocument();
XmlNodeList nl;
xd.Load(Server.MapPath("Ar ticles.xml "));
nl = xd.SelectNodes("/articles/ article[po sition() <=3]");
lstArticles.DataSource = nl;
lstArticles.DataBind();
}
aspx page code:
-------------------
<asp:datalist id="lstArticles" runat="server">
<itemtemplate>
<%# ((System.Xml.XmlNode)Conta iner.DataI tem).Selec tSingleNod e("id").In nerText %>
<br>
<%# ((System.Xml.XmlNode)Conta iner.DataI tem).Selec tSingleNod e("title") .InnerText %>
</itemtemplate>
</asp:datalist>
XmlFile
--------
<?xml version="1.0"?>
<articles>
<article>
<id>050</id>
<title>Article 050 Title</title>
<author>SomeGuy</author>
<dateposted>10/22/2003</da teposted>
<smalldescription>Small description</smalldescript ion>
<longdescription>Long Description</longdescripti on>
<code>Some code</code>
</article>
</articles>
Here is the modified code.
C# codebehind code
------------------------
private void Page_Load(object sender, System.EventArgs e)
{
XmlDocument xd = new XmlDocument();
XmlNodeList nl;
xd.Load(Server.MapPath("Ar
nl = xd.SelectNodes("/articles/
lstArticles.DataSource = nl;
lstArticles.DataBind();
}
aspx page code:
-------------------
<asp:datalist id="lstArticles" runat="server">
<itemtemplate>
<%# ((System.Xml.XmlNode)Conta
<br>
<%# ((System.Xml.XmlNode)Conta
</itemtemplate>
</asp:datalist>
XmlFile
--------
<?xml version="1.0"?>
<articles>
<article>
<id>050</id>
<title>Article 050 Title</title>
<author>SomeGuy</author>
<dateposted>10/22/2003</da
<smalldescription>Small description</smalldescript
<longdescription>Long Description</longdescripti
<code>Some code</code>
</article>
</articles>
ASKER
purpleblob you got all the points............
Many thanks
Glad to have helped
Glad to have helped
you can load the records into your DataView and then loop through the DataView and remove all but the rows you want to keep something like this.
for(int x = 0; x < dv.Rows.Count; x++)
{
if(x>=5)
{
dv.Rows[x].Delete();
}
}
I believe this will work unless there is a better way that someone else knows about :)