Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
ok here is what i am trying to do. I have created an aspx page that reads from an XML file and displays the data. On the page there are 4 buttons. First Contact, Previous, Next and Last Contact. I have the first three buttons working right, however, I can not get the Last Contact button to work at all. It should go to the last records in the XML file and display that data only. And for some reason, the First Contact buttons doesnt grey out if you click on it and you go to the first record. Please help me this is starting to hurt my brain.

C#
        protected void btnNext_Click(object sender, EventArgs e)
        {
            if (navigator.MoveToNext())     //moves to the next record.
            {
                DisplayData();      //displays the data when the next button is pushed.
                btnPrevious.Enabled = true;     //makes sure that the previous button is enabled at end of record.
            }
            else
            {
                btnNext.Enabled = false;        //disables the next button at end of contacts.
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            if (navigator.MoveToPrevious())                //move to the previous record.
            {
                DisplayData();      //displays the data when the previous button is pushed.
                btnNext.Enabled = true;     //makes sure that next is able to be pressed if previous is disabled.
            }
            else
            {
                btnPrevious.Enabled = false;        //this will disable the previous button.
            }
        }

        protected void btnFirstContact_Click(object sender, EventArgs e)
        {

        }
                
    }
}
Posted

Hi,

why don't using LINQ for reading XML-File?
I think you should save some time for reading a LINQ Article ;).

Maybe this works for you:

C#
var xml_path = @"C:\contacts.xml";
          var doc = XDocument.Load(xml_path);
          var last_entry = doc.Descendants("Contact").LastOrDefault();
          MessageBox.Show(last_entry.ToString());



Cheers.
 
Share this answer
 
v3
Comments
Member 8655982 17-Feb-12 18:19pm    
I will give this a shot and see how well it will work. I knwo that I have used XPathNavigator for the other buttons, and I knwo that I need to put a loop in the move to last button.
El_Codero 17-Feb-12 20:46pm    
I didn't know what you have done in your DisplayData() exactly, it sounded like "tell me how to get values from a XML-file".
Regards
Member 8655982 18-Feb-12 12:54pm    
the DisplayData() displays the data after the button is pushed. there are 4 different contacts and so far i can cycle through them all with the next previous buttons. I am just having troubles with that last button. I put the entire code below
here is the whole code so far,

C#
         XPathDocument document = new XPathDocument(path);       //made XPathDocument in order to navigate to each record.
                navigator = document.CreateNavigator();         //this creates the Navigator.
                navigator.MoveToRoot();     //moves to the Root of the XML file.
                navigator.MoveToChild("Contacts", String.Empty);        //moves to the Child of the Contacts Attribute.
                navigator.MoveToFirstChild();       //moves to the First Child of Contacts.
                
                DisplayData();      //displays the data that we need to show.

                Session["navigator"] = navigator;       //stores the nevigator to be reused in current location.
            }
            else
            {
                navigator = (XPathNavigator)Session["navigator"];       //retrieves the navigator.
            }
        }

        private void DisplayData()      //creates a method so we do not have to retype the same code repeatedly.
        {
            lableID.Text = navigator.GetAttribute("ID", String.Empty);      //displays the ID of the contact.
            navigator.MoveToFirstChild();       //we tell it to move to the Category Attribute.
            lableCategory.Text = navigator.Value;       //displays the category field.
            navigator.MoveToNext();     //tells it to move to the Name Attribute.
            navigator.MoveToFirstChild();       //tells it to move to the First Name.
            lableFirstName.Text = navigator.Value;      //displays the first name field.
            navigator.MoveToNext();     //tells it to move to the Last Name.
            lableLastName.Text = navigator.Value;       //displays the last name field.
            navigator.MoveToParent();       //tells it to go back to Contact.
            navigator.MoveToNext();     //tells it to go to Address.
            navigator.MoveToFirstChild();       //tells it to move to the Street, which is a child of Address.
            lableStreet.Text = navigator.Value;     //displays the street field.
            navigator.MoveToNext();     //moves to the next sibling.
            lableCity.Text = navigator.Value;       //displays the city field.
            navigator.MoveToNext();     //moves tot he next sibling.
            lableState.Text = navigator.Value;      //displays the state field.
            navigator.MoveToNext();     //moves tot he next sibling.
            lableZip.Text = navigator.Value;        //dispalys the zip code.
            navigator.MoveToParent();       //moves back to the parent.
            navigator.MoveToNext();     //moves to the next child.
            lablePhone.Text = navigator.Value;      //displays the phone number field.
            navigator.MoveToParent();       //enables to move to the next Contact node.
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            if (navigator.MoveToNext())     //moves to the next record.
            {
                DisplayData();      //displays the data when the next button is pushed.
                btnPrevious.Enabled = true;     //makes sure that the previous button is enabled at end of record.
            }
            else
            {
                btnNext.Enabled = false;        //disables the next button at end of contacts.
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            if (navigator.MoveToPrevious())                //move to the previous record.
            {
                DisplayData();      //displays the data when the previous button is pushed.
                btnNext.Enabled = true;     //makes sure that next is able to be pressed if previous is disabled.
            }
            else
            {
                btnPrevious.Enabled = false;        //this will disable the previous button.
            }
        }

        protected void btnFirstContact_Click(object sender, EventArgs e)
        {
            
        }
                
    }
}
 
Share this answer
 
ok I got it figured out, this is the solution that I came up with
C#
protected void btnMoveToLast_Click(object sender, EventArgs e)
        {
           do
           {
               if (navigator.NodeType == XPathNodeType.Element)
               {
                   if (navigator.HasChildren == true) 
                   {
                       navigator.MoveToFirstChild();

                       do 
                       {
                           DisplayData();
                       }
                       while (navigator.MoveToNext());
                   }
               }
           }
           while (navigator.MoveToNext());
           btnMoveToLast.Enabled = false;
           btnNext.Enabled = false;
           btnPrevious.Enabled = true;
           }
 
Share this answer
 
ah okay thanks for sharing your DisplayData method.
Do you thought about using MVC pattern for databinding?
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900