简体   繁体   中英

ListBox not displaying data from XML File

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>

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".

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 )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM