嵌套的foreach循环读取.xml并将对象写入列表

时间:2012-12-13 21:32:45

标签: c# xml debugging foreach

我有一个在form_load事件上执行的方法,它似乎可以正常省略一行。

private int ReadInPeople()
{
    XmlNodeList nodeList = m_xmlDoc.DocumentElement.ChildNodes;
    foreach (XmlNode PersonNode in nodeList)
    {
        Employee ccontact = new Employee();
        foreach (XmlNode PersonTag in PersonNode.ChildNodes)
        {
            switch (PersonTag.Name)
            {
                case "Employee":
                    ccontact.EmployeeNumber = PersonTag.FirstChild.Value;
                    break;
                case "FirstName":
                    ccontact.FirstName = PersonTag.FirstChild.Value;
                    break;
                case "LastName":
                    ccontact.LastName = PersonTag.FirstChild.Value;
                    break;
                default:
                    break;
            }
        }
        this.AddContact(ccontact);
    }
    return nodeList.Count;
}

AddContact方法将Employee对象添加到静态列表中;但是,行:

this.AddContact(ccontact);

没有被执行。

XML文件的示例:

<?xml version="1.0" encoding="utf-8"?>
<people>
  <person>
    <Employee>123456789</Employee>
    <FirstName>John</FirstName>
    <LastName>Smith</LastName>
  </person>
  <person>
    <Employee>987654321</Employee>
    <FirstName>Ellen</FirstName>
    <LastName>Wayne</LastName>
  </person>
</people>

我曾尝试设置断点和调试,果然,线路完全被跳过,好像它甚至都没有。

根据Alan的建议,我更改了PersonTag.FirstChild.Value,因为它试图引用一个不存在的ChildNode。

更新的工作方法:

private int ReadInPeople()
{
    XmlNodeList nodeList = m_xmlDoc.DocumentElement.ChildNodes;
    foreach (XmlNode PersonNode in nodeList)
    {
        Employee ccontact = new Employee();
        foreach (XmlNode PersonTag in PersonNode.ChildNodes)
        {
            switch (PersonTag.Name)
            {
                case "Employee":
                    ccontact.EmployeeNumber = PersonTag.InnerText;
                    break;
                case "FirstName":
                    ccontact.FirstName = PersonTag.InnerText;
                    break;
                case "LastName":
                    ccontact.LastName = PersonTag.InnerText;
                    break;
                default:
                    break;
            }
        }
        this.AddContact(ccontact);
    }
    return nodeList.Count;
}

3 个答案:

答案 0 :(得分:2)

你确定你不只是遇到例外吗?将您的逻辑包装在try / catch中,看看它是否会命中。

这样的行可能会导致空引用异常,并且可能是您没有访问该行代码的原因:

PersonTag.FirstChild.Value

我想在Employee,FirstName或LastName上的FirstChild上调用它会返回null,因为它们不包含子节点。

也许试试:

PersonTag.InnerText

答案 1 :(得分:2)

您可以使用LINQ to XML轻松解析xml:

XDocument xdoc = XDocument.Load(path_to_xml);   
var employees = xdoc.Descendants("person")
                    .Select(p => new Employee()
                    {
                        FirstName = (string)p.Element("FirstName"),
                        LastName = (string)p.Element("LastName"),
                        EmployeeNumber = (long)p.Element("Employee")
                    });

foreach (var ccontact in employees)
    this.AddContact(ccontact);

XmlDocument解决方案:

XmlNodeList nodeList = m_xmlDoc.DocumentElement.SelectNodes("person");
foreach (XmlNode PersonNode in nodeList)
{
    Employee ccontact = new Employee();
    ccontact.LastName = PersonNode["LastName"].InnerText;
    ccontact.FirstName = PersonNode["FirstName"].InnerText;
    ccontact.EmployeeNumber = PersonNode["Employee"].InnerText;
    this.AddContact(ccontact);
}

答案 2 :(得分:0)

Build -> Rebuild Solution

众所周知,它有时会有所帮助。

或许:Debug -> Exceptions... -> Thrown