在C#MVC中循环XML的问题

时间:2012-10-02 11:28:09

标签: c# xml linq linq-to-xml

您好,我正在尝试将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;
    }

当我试图循环时,我得到一个null异常,我做错了什么?

这是我的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>

3 个答案:

答案 0 :(得分:3)

Beebul,他们Element不是Attribute s

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()

答案 1 :(得分:2)

看起来您的一个或多个属性丢失了。 items.Attribute(...)返回null,并在其上调用Value会导致NPE *

由于执行 deferred ,在您开始循环result之前,调用才会发生。

要找到导致问题的属性,请删除除Attribute(...)之外的所有ID调用,验证是否发生了崩溃,并开始逐个添加属性,直到崩溃恢复。

<小时/> * 看到您添加到问题中的XML后,似乎缺少所有属性!这是一个讨论差异的link to a short article

答案 2 :(得分:0)

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