Solved

How to Select Next node using linq in xml?

Posted on 2014-02-26
3
499 Views
Last Modified: 2016-02-10
hi guys... I got this problem...
I've created 4 buttons that their names are first, next, previous, last.
what i want is that when the user clicks the first button the application throws a messageBox showing the values of the first book and when the user clicks next to show in another messageBox with the next book of my xml document, the same with previous and last...
i got this xml:

<?xml version="1.0" encoding="utf-8"?>
<books>
  <book>
    <title>Beginning Visual C#</title>
    <author>Karli Watson</author>
    <code>7582</code>
  </book>
  <book>
    <title>Professional C# 2nd Edition</title>
    <author>Simon Robinson</author>
    <code>7043</code>
  </book>
  <book>
    <title>Beginning XML</title>
    <author>Fawcett</author>
    <code>1500</code>
  </book>
  <book>
    <title>Silverlight 5</title>
    <author>Diaz</author>
    <code>1501</code>
  </book>
  <book>
    <title>Professional Silverlight 4 in c#</title>
    <author>MacDonald</author>
    <code>1502</code>
  </book>
</books>

Open in new window



and i got this code:

//first button
            XElement xEle = XElement.Load(@"C:\books.xml");

            var emp1 = xEle.Descendants("book").ElementAt(0);
            

            MessageBox.Show("Title: " + emp1.Element("title").Value + System.Environment.NewLine + "Author: " + emp1.Element("author").Value + System.Environment.NewLine + "Code: " + emp1.Element("code").Value);

Open in new window


what can i do for the next button?
0
Comment
Question by:Conrado ZAVALA
  • 2
3 Comments
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 39889442
What you want to do is maintain a reference to the currently-selected book node. That is more or less what your emp1 variable is doing. You would, however, need to change the use of var to the actual type name because you are going to need to widen the scope of that variable to be class-level. Once you do that, you can use this reference to navigate the document with your other buttons. For example, "Next" might be implemented as:

protected void btnNext_Click(object sender, EventArgs e)
{
    emp1 = emp1.NextNode;  // next sibling node, which (ideally) should be another <book>
}

Open in new window


The "Previous" button could be implemented in a similar fashion, using the PreviousNode method instead.
0
 

Author Comment

by:Conrado ZAVALA
ID: 39889508
what would be the actual type name of var?? i don't get it yet... xD
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 39889905
XElement
0

Featured Post

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

809 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