0

您好我正在尝试将 XML 文件解析为联系人列表,但遇到问题:

    public List<ContactModel> GetContacts()
    {

        var doc = XDocument.Load(HttpContext.Current
                                            .Server
                                            .MapPath(@"..\App_Data\Contacts.xml"));

        var result = from items in doc.Descendants("Directory")
                     select new ContactModel()
                                {
                                    Id = items.Attribute("ID").Value,
                                    FirstName = items.Attribute("FirstName").Value,
                                    LastName = items.Attribute("LastName").Value,
                                    Telephone = items.Attribute("Telephone").Value,
                                    Email = items.Attribute("Email").Value,
                                    Room = items.Attribute("Room").Value,
                                    Building = items.Attribute("Building").Value,
                                    Location = items.Attribute("Location").Value
                                };

        List<ContactModel> contactList = new List<ContactModel>();
        foreach (var item in result)
        {
            contactList.Add(item);
        }

        return contactList;
    }

尝试循环时出现空异常,我做错了什么?

这是我的 XML

<?xml version="1.0" standalone="yes"?>
<ContactDirectory>
  <Directory>
    <ID>1</ID>
    <FirstName>Peter</FirstName>
    <LastName>Sutt</LastName>
    <Telephone>777888</Telephone>
    <Email>pett@gmail.com</Email>
    <Room>3.44</Room>
    <Building>Westside</Building>
    <Location>Leeds</Location>
  </Directory>
  <Directory>
    <ID>2</ID>
    <FirstName>Fred</FirstName>
    <LastName>West</LastName>
    <Telephone>1234</Telephone>
    <Email>fred@west.com</Email>
    <Room>1.23</Room>
    <Building>Cromwell St</Building>
    <Location>Gloster</Location>
  </Directory>
  <Directory>
</ContactDirectory>
4

3 回答 3

3

比布尔,他们Element不是Attribute

var contactList = (from items in doc.Descendants("Directory")
                select new ContactModel()
                {
                    Id = items.Element("ID").Value,
                    FirstName = items.Element("FirstName").Value,
                    LastName = items.Element("LastName").Value,
                    Telephone = items.Element("Telephone").Value,
                    Email = items.Element("Email").Value,
                    Room = items.Element("Room").Value,
                    Building = items.Element("Building").Value,
                    Location = items.Element("Location").Value
                })
                .ToList();

PS:您无需遍历您result的列表即可。您可以使用ToList()

于 2012-10-02T11:46:35.063 回答
2

您的一项或多项属性似乎缺失。items.Attribute(...)返回null,并调用Value它会导致 NPE *

由于执行被延迟,调用不会发生,直到您开始循环遍历result.

要找到导致问题的属性,请删除对Attribute(...)except 的所有调用ID,验证崩溃没有发生,然后开始一一添加属性,直到崩溃再次出现。


*在看到您添加到问题中的 XML 后,似乎所有属性都丢失了!这是讨论差异的简短文章的链接。

于 2012-10-02T11:32:03.480 回答
0

Linq to XML API 具有隐式转换,以帮助处理从 .Value 返回的可能的空引用。尝试类似的东西(string) items.Attribute("Room")

于 2012-10-02T11:49:03.903 回答