简体   繁体   English

ListBox不显示XML文件中的数据

[英]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. 问题:我已经将数据保存到XML文件中,但是当我重新打开应用程序时,XML文件中的数据应该填充到列表框中,但是什么也没有显示。 I've been at this for 2 hours and cannot find the problem. 我已经在这里待了两个小时了,找不到问题。

My code to load xml file : 我的代码加载xml文件:

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 编辑: http//pastebin.com/YwPj0Nxc

Couldn't find a good way to format that on this site, but that's the XML file. 在此站点上找不到格式化该格式的好方法,但这就是XML文件。

Smurf Edit : Adding pastebin XML Smurf Edit :添加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. 不查看XML文件本身就无法回答。 Make sure all the attributes and names match with the query. 确保所有属性和名称都与查询匹配。 Make sure the TicketList has data before you bind it to the ListBox. 在将TicketList绑定到ListBox之前,请确保它具有数据。 You need extensive debugging. 您需要大量调试。 I think the problem is your Linq query. 我认为问题是您的Linq查询。 You need to simplify your LINQ query. 您需要简化LINQ查询。

Update: Your element names are not matching. 更新:您的元素名称不匹配。 I think the LINQ is also missing its outer "Tickets" element. 我认为LINQ也缺少其外部的“ Tickets”元素。 It should go each element inside "Tickets". 它应该放在“票证”中的每个元素上。 It says "TicketNum", but the XML has "TicketNumber". 它说“ TicketNum”,但是XML有“ TicketNumber”。

Looks like there is a misspelling in the XML vs the LINQ: 看起来XML与LINQ拼写错误:

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

Should be: 应该:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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