Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to Select Next node using linq in xml?

Posted on 2014-02-26
3
Medium Priority
?
536 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

730 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