Solved

Reading Top Five records from XML file ......

Posted on 2003-10-30
8
365 Views
Last Modified: 2010-04-16
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</dateposted>
        <smalldescription>Small description</smalldescription>
        <longdescription>Long Description</longdescription>
        <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();
}




0
Comment
Question by:adamullah
  • 3
  • 3
  • 2
8 Comments
 
LVL 2

Expert Comment

by:ericsDev
ID: 9654007
**UNTESTESTED**

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 :)
0
 
LVL 6

Expert Comment

by:purpleblob
ID: 9658961
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


0
 
LVL 2

Expert Comment

by:ericsDev
ID: 9659108
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
0
 
LVL 6

Accepted Solution

by:
purpleblob earned 120 total points
ID: 9659641
Very good point re:support of the function position by MS

So I thought I should try this in .NET. It appears to work fine except I made a small mistake in the XPath statement it should have read

//article[position() <= 5]

Thus becomes

System.Xml.XmlNodeList nodes = xmldoc.SelectNodes("//article[position() <= 5]");

The /* will return the inner items within the top 5 elements as opposed to the elements themselves - whoops, sorry about that
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 2

Expert Comment

by:ericsDev
ID: 9659656
purpleblob: good work, I will keep this method in mind.
0
 

Author Comment

by:adamullah
ID: 9668707
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("Articles.xml"));

      nl = xd.SelectNodes("/articles/article[position() <=3]");
      
      lstArticles.DataSource = nl;
      lstArticles.DataBind();

}


aspx page code:
-------------------
<asp:datalist id="lstArticles" runat="server">
      <itemtemplate>
            <%# ((System.Xml.XmlNode)Container.DataItem).SelectSingleNode("id").InnerText %>
            <br>
            <%# ((System.Xml.XmlNode)Container.DataItem).SelectSingleNode("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</dateposted>
        <smalldescription>Small description</smalldescription>
        <longdescription>Long Description</longdescription>
        <code>Some code</code>
    </article>
</articles>
 
 
0
 

Author Comment

by:adamullah
ID: 9668712
purpleblob you got all the points............
0
 
LVL 6

Expert Comment

by:purpleblob
ID: 9670681
Many thanks

Glad to have helped
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# GridRow get Old/New Value 1 55
C# application error "Parameter Is Not Valid". 3 79
Error in JQuery 5 38
Exit the loop 4 35
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now