0

Problem : I have data I've saved into an XML file, but when I re-open the application, the data in the XML file should be populating my listbox but nothing shows up. I've been at this for 2 hours and cannot find the problem.

My code to load xml file :

public void Load()
{
    XDocument myDoc = XDocument.Load(".../.../parking.xml");

    var ticks = from xElem in myDoc.Descendants("Ticket")
               select new Ticket
               {
                   TimeIn = Convert.ToDateTime(xElem.Element("TimeIn").Value),
                   TicketNum = Convert.ToInt32(xElem.Element("TicketNumber").Value),
               };

    this.Clear();

    AddRange(ticks);
}

And my code to try to populate the listbox :

{
        newList = new TickList();

        newList.Load();

        foreach (var nTick in newList)
        {
            spotList.Items.Add(nTick.ToString());
        }
    }

EDIT : http://pastebin.com/YwPj0Nxc

Couldn't find a good way to format that on this site, but that's the XML file.

Smurf Edit: Adding pastebin XML

<?xml version="1.0" encoding="utf-8"?>
<Tickets>
  <Ticket>
    <TicketNum>1</TicketNum>
    <TimeIn>2012-10-11T17:49:49.896445-05:00</TimeIn>
  </Ticket>
  <Ticket>
    <TicketNum>2</TicketNum>
    <TimeIn>2012-10-11T17:49:50.2714664-05:00</TimeIn>
  </Ticket>
  <Ticket>
    <TicketNum>3</TicketNum>
    <TimeIn>2012-10-11T17:49:50.4304755-05:00</TimeIn>
  </Ticket>
  <Ticket>
    <TicketNum>4</TicketNum>
    <TimeIn>2012-10-11T17:49:50.5944849-05:00</TimeIn>
  </Ticket>
</Tickets>
3
  • are you sure your TicketList is being populate? Try putting a break point in after load and looking at the data stored in newList. Commented Oct 11, 2012 at 23:22
  • You are populating the list box correctly, your problem is in how you load the xml file. If you provide that to us we can tell you where you went wrong in the reading. Commented Oct 11, 2012 at 23:27
  • Both are going in there, I've overriden ToString() to include both. Commented Oct 12, 2012 at 0:12

2 Answers 2

1

Can't answer without looking at the XML file itself. Make sure all the attributes and names match with the query. Make sure the TicketList has data before you bind it to the ListBox. You need extensive debugging. I think the problem is your Linq query. You need to simplify your LINQ query.

Update: Your element names are not matching. I think the LINQ is also missing its outer "Tickets" element. It should go each element inside "Tickets". It says "TicketNum", but the XML has "TicketNumber".

Sign up to request clarification or add additional context in comments.

1 Comment

Corrected that TicketNum / TicketNumber mistake, and now the listbox will load the first record, but it places a 1 as a seperate item in the listbox, then it places the actual first record on the second line. And doesn't load the last 3 records from the file. Also, I am new to LINQ and XML and do not understand what you mean when you say my LINQ is missing its outer "Tickets" element, can you elaborate? Thank you.
0

Looks like there is a misspelling in the XML vs the LINQ:

Convert.ToInt32( xElem.Element( "TicketNumber" ).Value )

Should be:

Convert.ToInt32( xElem.Element( "TicketNum" ).Value )

2 Comments

Yeah, iefpw mentioned that and I corrected. But it has lead to yet another problem. Now the listbox will display : 1 (on one line) and then the actual first record on the next line. But fails to load the last 3 records from the xml.
That indeed fixed it completely. Just had to clear the xml and rebuild it for some reason before it would register. All clear!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.