使用循环序列化为XML文档 [英] Serializing into XML document with a loop

查看:66
本文介绍了使用循环序列化为XML文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一个对象序列化为一个xml文档,但是当我放入一个循环时我一直收到这个错误。







在HotelReservations.dll中发生'System.NullReferenceException'类型的未处理异常



I am trying to serialize an object into a xml document, but i keep getting this error when i put in a loop.



"An unhandled exception of type 'System.NullReferenceException' occurred in HotelReservations.dll"

public  class Inventory
    {
        StreamWriter sw;
        XmlSerializer serial;
        const string Inventory_Filename = @"..\..\roominventory.xml";

        public List<InventoryType> Inventoryttype { get; set; }


        public Inventory() { }
        public void CreateInvenList()
        { 
            Random rnd = new Random();
            int num = rnd.Next(1, 10);
            DateTime end = new DateTime();

            for( DateTime date = DateTime.Now; date < end; date.AddDays(3) )
          {
                foreach (InventoryType inventoryttype in Inventoryttype)
                {
                    List<InventoryType> Inventoryttype = new List<InventoryType>();

                    Inventoryttype.Add(new InventoryType(date, "123", "KB", num));

                }


            }


                serial = new XmlSerializer(Inventoryttype.GetType());
                sw = new StreamWriter(Inventory_Filename);
                serial.Serialize(sw, Inventoryttype);
                sw.Close();
            

        }
    }
}





什么我试过了:



i尝试将序列化代码放在循环中,但它没有做任何事情。如果我取出所有循环,只是序列化一个对象,它的工作原理。但是当我把它放在一个循环中时,错误就出现了。



What I have tried:

i try putting the serialize code inside the loop, but it just does not do anything. if i take out all the loop, and just serialize the one object, it works. but the moment i put it in a loop, the error comes up.

推荐答案

你的代码很乱,它甚至不应该编译< br $> b $ b

You've got quite the mess with your code and it shouldn't even compile

public List <InventoryType> {get;set;}





是一个名为与本地变量相同的属性





Is a property named the same as a local variable

List<InventoryType> Inventoryttype = new List<InventoryType>();





您的空例外是因为你要序列化一个尚未申报的清单。







Your null exception is coming from the fact that you are tring to serialize a list that has not been declared.


serial = new XmlSerializer(Inventoryttype.GetType());
           sw = new StreamWriter(Inventory_Filename);
           serial.Serialize(sw, Inventoryttype);
           sw.Close();





您的.Serialize方法正在尝试序列化您尚未初始化的Inventoryttype属性,因此为null,这将解释你的空引用异常。实际上,看看你的代码,你也试图在你的for循环中添加项目到这个列表,.Add可能是异常首次发生的地方。



另外,你没有在for循环中进行序列化,你在for循环之外进行序列化,不确定你是否意识到这一点。



所以你的修复是要么在CreateInvenList方法的顶部调用以下代码





Your .Serialize method is trying to Serialize the Inventoryttype property that you have no initialized and is therefore null which would explain your null reference exception. Actually, looking at your code, you are also trying to add items to this list which inside your for loop, the .Add is probably where the exception is first occurring.

Also, you aren't serializing inside of the for loop, you are serializing outside of the for loop, not sure if you realize that.

So your fix is to either call the following code at the top of your CreateInvenList method

this.Inventoryttype = new List<InventoryType>();





或者只是将您的属性更改为字段





Or simply change your property to a field

public List<InventoryType> Inventoryttype = new List<InventoryType>();


这篇关于使用循环序列化为XML文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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