简体   繁体   English

从多个XML文件读取并在ListBox问题中显示数据

[英]Reading from multiple XML files and displaying data in a ListBox issue

So I've been working on a project and I've hit a snag. 因此,我一直在从事一个项目,遇到了麻烦。 I need to read employee data from XML files, then display that data in a ListBox. 我需要从XML文件读取员工数据,然后在ListBox中显示该数据。

NB: "tempFullName" is an empty string assigned at the start of the script. 注意:“ tempFullName”是在脚本开始时分配的空字符串。 It is never reset to null. 永远不会重置为null。

void NewDateItemLoad(object sender, EventArgs e)
    {

        string path = Application.StartupPath.ToString() + "\\Users";
        int fCount = Directory.GetFiles(path, "*.xml", SearchOption.AllDirectories).Length;
        for(int i = 0;i<fCount;i++)
        {
            String[] filePaths = Directory.GetFiles(Application.StartupPath + "\\Users\\");
            XmlDocument xmlFile =new XmlDocument();
            xmlFile.Load(filePaths[i]);

            foreach(XmlNode node in xmlFile.SelectNodes("//Account"))
            {
                XmlNode nodeFirstName = node.SelectSingleNode("FirstName");
                XmlNode nodeLastName = node.SelectSingleNode("LastName");

                if(nodeFirstName.Value != "" && nodeLastName.Value != ""){
                    tempFullName = nodeFirstName.Value + nodeLastName.Value;
                }

            }


            lstWorkers.Items.Add(tempFullName);
            lstWorkers.Items.Add("-------------------"); //Temporary Divide


        }
    }

Each employee has their own XML file because I set my program to use that format because I found it easier before I understood correctly how to interact with XML files. 每个员工都有自己的XML文件,因为我将程序设置为使用该格式,因为在正确理解如何与XML文件交互之前,我发现它更容易。 I use a similar process for searching through login details and that works perfectly fine, sadly the names do not show in the listbox. 我使用类似的过程搜索登录详细信息,并且工作得很好,可惜名称没有显示在列表框中。

http://puu.sh/g5cjK/7132fe251f.png This is the result of the code. http://puu.sh/g5cjK/7132fe251f.png这是代码的结果。 http://puu.sh/g5cWQ/254ecb4041.png An example of the XML files I'm using. http://puu.sh/g5cWQ/254ecb4041.png我正在使用的XML文件的示例。

If anyone could help make the employee names show correctly, or point out where I've gone wrong that'd be greatly appreciated. 如果有人可以帮助使员工姓名正确显示,或指出我出了错的地方,将不胜感激。

The names are independent text nodes within the element so rather than reading .Value 名称是元素内的独立文本节点,因此而不是读取.Value

if (nodeFirstName.InnerText != "" && nodeLastName.InnerText != "")

If there are multiple elements, you need to add to the list within the loop else you will always end up with the last one. 如果有多个元素,则需要在循环中添加到列表中,否则总是以最后一个元素结束。

You may like to stuff all the XML in a single file. 您可能希望将所有XML填充到一个文件中。

If you want something fully automated and that will work simply with no heavy logic, I suggest to go with xmlSerialiser. 如果您想要完全自动化的东西并且不需要繁琐的逻辑就可以工作,那么我建议您使用xmlSerialiser。 Go to that link for more informations. 请转到该链接以获取更多信息。

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

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