使用XElement时发生NullReferenceException [英] NullReferenceException while using XElement

查看:113
本文介绍了使用XElement时发生NullReferenceException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种方法可以编辑xml文件:

I have a method that is suppose to edit a xml file:

    public void EditItem(Item item, string xml)
    {
        Data = XDocument.Load(HttpContext.Current.Server.MapPath("~/App_Data/Items/" + xml + ".xml"));

        XElement node = Data.Root.Elements("item").Where(i => (string)i.Element("ID") == item.ID).FirstOrDefault();

        node.SetElementValue("ID", item.ID);
        node.SetElementValue("Name", item.Name);
        node.SetElementValue("Type", item.Type);
        node.SetElementValue("Kr", item.Kr);
        node.SetElementValue("Euro", item.Euro);

        Data.Save(HttpContext.Current.Server.MapPath("~/App_Data/Tables/" + xml + ".xml"));
    }

我通过在控制器中的try/catch收到此验证错误:对象引用未设置为对象的实例."通过一些调试,我发现节点"为空,即使数据"包含来自xml的所有正确数据,并且model.ID是正确的.

I get this validation error thru a try/catch in my controller: "Object reference not set to an instance of an object." Through some debugging, I found that "node" is null, even though "Data" contains all the right data from the xml, and the model.ID is correct.

更奇怪的是,我让它在另一个仓库中工作,其中xml不是动态的,并且XDocument obj被加载到构造函数中.

the wierd thing is, that I have it working in another repo where the xml isnt dynamic, and the XDocument obj is loaded in the constructor.

任何想法是什么原因引起的?也许还有一些解决方法的想法.

Any ideas what causes it? Or maybe some ideas on a workaround.

更新. Xml代码段:

Update. Xml snippet:

<?xml version="1.0" encoding="utf-8"?>
    <catagory id="0">
      <module>
        <item>
          <ID>101</ID>
          <Name>ClassicoTable(35x100x100)</Name>
          <Type>Model</Type>
          <Kr>0</Kr>
          <Euro>0</Euro>
          <DataType>ClassicoTableA</DataType>
        </item>
        <item>
          <ID>100</ID>
          <Name>ClassicoTable(102x100x140)</Name>
          <Type>Model</Type>
          <Kr>0</Kr>
          <Euro>0</Euro>
          <DataType>ClassicoTableB</DataType>
        </item> 



       ......

      </module>
    </catagory id="0">

推荐答案

好的,找到了解决方案.我开始怀疑为什么@Kim为什么要从我的xml中提取一个片段.因此,我认为Data.Root.Element()可能不是正确的方法.因此,我尝试使用Descendants()代替,这确实有效.为什么?我没有线索.原因如下:

Okay found a solution. I started wondering why @Kim wanted a snippet from my xml. So it made me think that Data.Root.Element() maybe wasnt the right way. So I tried with Descendants() instead, and that actually works. Why? I have no clue. Heres why:

在另一个仓库中,我在仓库的构造函数中有XDocument.load().我认为那会很好,因为那样我就不必在所有CRUD方法中重复相同的代码.但是,因为我希望xml动态,并且构造函数不接受参数,所以我认为这种方式(原始问题)会很好.这里是静态"存储库中的代码:

In another repo I have the XDocument.load() in the constructor of the repo. I thought that would be good, because then I wouldnt have to repeat the same code in all the CRUD methods. But, as I wanted the xml dynamic, and the constructor doesnt accept parameters, I thought this way (original question) would be fine. Here the code from the "static" repo:

//Constructor
public CubeRepository()
{
    allCubes = new List<Cube>();

    CubeData = XDocument.Load(HttpContext.Current.Server.MapPath("~/App_Data/Cubes/Cubep10p11.xml"));
    var cubes = from cube in CubeData.Descendants("item")
                select new Cube(cube.Element("ID").Value, 
                    cube.Element("Name").Value, 
                    cube.Element("Type").Value,
                    (int)cube.Element("Kr"),
                    (int)cube.Element("Euro"));

    allCubes.AddRange(cubes.ToList<Cube>());
}

在该存储库的编辑方法中:

And in my edit method of that repo:

public void EditCube(Cube cube)
{
    XElement node = CubeData.Root.Elements("item").Where(i => (string)i.Element("ID") == cube.ID).FirstOrDefault();
    node.SetElementValue("ID", cube.ID);
    node.SetElementValue("Name", cube.Name);
    node.SetElementValue("Type", cube.Name);
    node.SetElementValue("Kr", cube.Kr);
    node.SetElementValue("Euro", cube.Euro);

    CubeData.Save(HttpContext.Current.Server.MapPath("~/App_Data/Cubes/Cubep10p11.xml"));
}

即使我正在使用CubeData.Root.Elements("item"),它的工作方式也像我的魅力.注意:Elements插入了Descendants. xml文件的结构相同.

And that works like I charm, even though im using CubeData.Root.Elements("item"). Note: Elements insted of Descendants. The structure of the xml files are identical.

这篇关于使用XElement时发生NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆