Solved

How to Select Next node using linq in xml?

Posted on 2014-02-26
3
479 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 74

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
Comment Utility
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
Comment Utility
what would be the actual type name of var?? i don't get it yet... xD
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
Comment Utility
XElement
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 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

13 Experts available now in Live!

Get 1:1 Help Now