使用“对象引用未设置为对象的实例”读取xml异常

时间:2017-08-24 10:29:21

标签: c# xml linq-to-xml

我试图读取像这样的xml配置文件:

public class ProductDb
{
    public ObservableCollection<Product> GetProducts()
    {
        ObservableCollection<Product> _products = new ObservableCollection<Product>();
        XDocument doc = XDocument.Load(@".\Config\Chip.xml");
        foreach(XElement productRow in doc.Root.Elements("Chip"))
        {
            var m = new Product(productRow.Element("ChipName").Value, productRow.Element("Series").Value, 
                productRow.Element("Type").Value,Convert.ToUInt32(productRow.Element("FlashAddress").Value), 
                Convert.ToUInt32(productRow.Element("PageCount").Value), Convert.ToUInt32(productRow.Element("PageSize").Value),
                Convert.ToUInt32(productRow.Element("RamAdd").Value), Convert.ToUInt32(productRow.Element("RamLength").Value));
            _products.Add(m);
        }
        return _products;
    }
}

Product的类定义如下:

private string _chipName;
public string ChipName
{
    get { return _chipName; }
    set
    {
        _chipName = value;
        NotifyPropertyChanged("ChipName");
    }
}

private string _series;
public string Series{  ... }

private string _type;
public string Type{ ... }

private UInt32 _flashAddress;
public UInt32 FlashAddress{ ... }

private UInt32 _pageCount;
public UInt32 PageCount{ ... }

private UInt32 _pageSize;
public UInt32 PageSize{ ... }

private UInt32 _ramAddress;
public UInt32 RamAddress{ ... }

private UInt32 _ramLength;
public UInt32 RamLength{ ... }



public Product() { }

private string FlashInfo;
private string RamInfo;

public Product(string _chipName, string _series, string _type, UInt32 _flashAddress, 
               UInt32 _pageCount, UInt32 _pageSize,UInt32 _ramAddress, UInt32 _ramLength)
{
    ChipName = _chipName;
    Series = _series;
    Type = _type;

    FlashInfo = Convert.ToString(_flashAddress, 16).ToUpper().PadLeft(8, '0') + "---0x" +
        Convert.ToString((_flashAddress + _pageCount * _pageSize - 1), 16).ToUpper().PadLeft(8, '0') +
        "(" + Convert.ToString((_pageCount * PageSize / 1024), 10) + ")KB";

    RamInfo = Convert.ToString(_ramAddress, 16).ToUpper().PadLeft(8, '0') + "---0x" +
        Convert.ToString((_ramAddress + _ramLength - 1), 16).ToUpper().PadLeft(8, '0') + "(" +
        Convert.ToString((_ramLength / 1024), 10) + ")KB";
}

当我试图通过:

public ObservableCollection<Product> ProductsList { get; set; }
ProductDb pd = new ProductDb();
private void GetProductsList()
{
    try
    {
        ProductsList = new ObservableCollection<Product>(pd.GetProducts());
    }
     catch
    {
    }
}

它捕获了"Object reference not set to an instance of an object."我该如何解决这个问题的异常?提前谢谢!

--------------------------的更新 ------------- -------------------------- Xml文件:

<System>
    <Chip>
       <ChipName>Hxxxxxxxx</ChipName>
       <Series>CM0+</Series>
       <Type>0</Type>
       <FlashAddress>00000000</FlashAddress>
       <PageCount>256</PageCount>
       <PageSize>512</PageSize>
       <RAMAddress>20000000</RAMAddress>
       <RAMLength>1800</RAMLength>
   </Chip>
</System>

0 个答案:

没有答案