【问题标题】:Why am I getting Null Referenfe exception while trying to read data from XML file?为什么在尝试从 XML 文件中读取数据时出现 Null Referenfe 异常?
【发布时间】:2013-05-01 15:11:01
【问题描述】:

我有一个名为 Customerx.xml 的 XML 文件。它的父标签为 .它有很多。

我的客户类别定义为:

public class Customer
{
  private List<Customer> customerList;

  public string CustomerID { get; set; }
  public string CompanyName { get; set; }
  public string Address { get; set; }
  public string City { get; set; }
  public string Region { get; set; }
  public string PostalCode { get; set; }
  public string Country { get; set; }
  public string Phone { get; set; }
  public string Fax { get; set; }

  public List<Customer> GetCustomerList()
  {
     if (customerList == null)
        CreateCustomerLists();

     return customerList;
  }

  private void CreateCustomerLists()
  {
     var list = from e in XDocument.Load("Customers.xml").Root.Elements("customer")
                 select new Customer
                            {
                                CustomerID = (string) e.Element("id"),
                                CompanyName = (string) e.Element("name"),
                                Address = (string) e.Element("address"),
                                City = (string) e.Element("city"),
                                Region = (string) e.Element("region"),
                                PostalCode = (string) e.Element("postalcode"),
                                Country = (string) e.Element("country"),
                                Phone = (string) e.Element("phone"),
                                Fax = (string) e.Element("fax"),
                            };

      customerList = list.ToList();
  }
}

XML 中的数据是:

<?xml version="1.0"?>
<customers>
  <customer>
    <id>ALFKI</id>
    <name>Alfreds Futterkiste</name>
    <address>Obere Str. 57</address>
    <city>Berlin</city>
    <postalcode>12209</postalcode>
    <country>Germany</country>
    <phone>030-0074321</phone>
    <fax>030-0076545</fax>
  </customer>
  <customer>
    <id>ANATR</id>
    <name>Ana Trujillo Emparedados y helados</name>
    <address>Avda. de la Constitución 2222</address>
    <city>México D.F.</city>
    <postalcode>05021</postalcode>
    <country>Mexico</country>
    <phone>(5) 555-4729</phone>
    <fax>(5) 555-3745</fax>
  </customer>

在CreateCustomerLists() 方法中,我在从 xlm 文件读取数据时得到一个空引用执行?为什么会这样?我怎样才能摆脱它?

【问题讨论】:

  • 哪一行?您的 XML 是什么样的?一个简短但完整的程序来演示这个问题真的很有帮助......
  • NullReferenceException 的几乎所有情况都是相同的。请参阅“What is a NullReferenceException in .NET?”获取一些提示。
  • @JonSkeet,我已经编辑了我的问题。现在我也提供了两个 xml 数据。我在 var list = from ... CreateCustomerLists() 方法上遇到错误的那一行。
  • 在您的 xml 中找不到区域。这可能是空引用的罪魁祸首
  • 你能给出 stacktrace 的确切例外吗?

标签: c# xml linq nullreferenceexception


【解决方案1】:

回答您的其他问题:如果您在 Visual Studio 中从 Debug 运行,则调试文件夹将是它用于查找文件的相对路径的开始。您需要包含的任何文件都应放在其中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多