Solved

How to Select Next node using linq in xml?

Posted on 2014-02-26
3
508 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

756 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