使用NetDataContractSerializer的ReadObject时出错“Value Can not null”

时间:2015-06-11 02:53:34

标签: c# xml serialization

我正在尝试使用NetDataContractSerializer来反序列化由NetDataContractSerializer序列化的XML。

我正在使用NetDataContractSerializer而不是DataContractSerializer,因为我正在序列化包含一些多维数组的类。 我用一些W3验证器验证了XML文件并将其传递。

这是反序列化部分

using (FileStream reader = new FileStream(filePath,FileMode.Open))
            {
                NetDataContractSerializer ser = new NetDataContractSerializer();
                Universe loadedUniverse = (Universe)ser.ReadObject(reader);
            }

这是我得到的例外

  

值不能为空。参数:来源

我试图深入调试它,但不明白到底出了什么问题。 看起来FileStream正确地获取了文件的内容。

还尝试使用XmlReader而不是FileStream,得到了同样的错误。使用XmlReader我看到它的序列化类的类型也很好,所以我排除找不到文件或类似的东西。

这是代码的序列化部分

NetDataContractSerializer ser =
            new NetDataContractSerializer();

        using (FileStream file = new FileStream(target, FileMode.Create))
        {
            ser.WriteObject(file, universe);
        }

Here is the xml file(抱歉找不到在线XML共享工具)

我试图解决这个问题的方式有什么问题吗? 非常感谢你的帮助!

2 个答案:

答案 0 :(得分:1)

请尝试以下代码。我将XML第一行添加到我的文件中。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;

using System.IO;

namespace ConsoleApplication33
{
    class Program
    {
        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        {
            XDocument doc = XDocument.Load(FILENAME);
            XElement m_AllCells = doc.Descendants().Where(x => x.Name.LocalName == "AllCells").FirstOrDefault();
            AllCells allCells = new AllCells(m_AllCells); 
        }

    }
    public class AllCells
    {
        public int size {get;set;}
        public int version {get;set;}
        public Items items { get; set; }

        public AllCells(XElement m_AllCells)
        {
            size = int.Parse(m_AllCells.Elements().Where(x => x.Name.LocalName == "_size").FirstOrDefault().Value);
            version = int.Parse(m_AllCells.Elements().Where(x => x.Name.LocalName == "_version").FirstOrDefault().Value);

            XElement childItem = m_AllCells.Elements().Where(x => x.Name.LocalName == "_items").FirstOrDefault();
            if (childItem != null)
            {
                items = new Items(childItem);
            }
        }
    }
    public class Items
    {
        public int? iD { get; set; }
        public int? size { get; set; }
        public List<Cell> cells {get; set;}
        public Items(XElement items)
        {
            XAttribute newId = items.Attributes().Where(x => x.Name.LocalName == "Id").FirstOrDefault();
            if (newId == null)
            {
                iD = null;
            }
            else
            {
                iD = int.Parse(newId.Value);
            }
            XAttribute newSize = items.Attributes().Where(x => x.Name.LocalName == "Size").FirstOrDefault();
            if (newSize == null)
            {
                size = null;
            }
            else
            {
                size = int.Parse(newSize.Value);
            }
            List<XElement> childCells = items.Elements().Where(x => x.Name.LocalName == "Cell").ToList();
            if (childCells == null)
            {
                cells = null;
            }
            else
            {
                cells = new List<Cell>();
                foreach (XElement childCell in childCells)
                {
                    cells.Add(new Cell(childCell)); 
                }
            }
        }

    }
    public class Cell
    {
        public bool nil { get; set; }
        public int? m_ref {get;set;}
        public int? iD {get; set;}
        public Neighbor neighbor { get; set; }
        public string state { get; set; }

        public Cell(XElement cell)
        {
            XAttribute newNil = cell.Attributes().Where(x => x.Name.LocalName == "nil").FirstOrDefault();
            if (newNil == null)
            {
                nil = false;
            }
            else
            {
                nil = (cell.Value == "true")? true : false;
            }
            XAttribute newRef = cell.Attributes().Where(x => x.Name.LocalName == "Ref").FirstOrDefault();
            if (newRef == null)
            {
                m_ref = null;
            }
            else
            {
                m_ref  = int.Parse(newRef.Value);
            }
            XAttribute newId = cell.Attributes().Where(x => x.Name.LocalName == "Id").FirstOrDefault();
            if (newId == null)
            {
                iD = null;
            }
            else
            {
                iD = int.Parse(newId.Value);
            }
            XElement newState = cell.Elements().Where(x => x.Name.LocalName == "State").FirstOrDefault();
            if (newState == null)
            {
                state = string.Empty;
            }
            else
            {
                state = newState.Value;
            }
            XElement newNeighbors = cell.Elements().Where(x => x.Name.LocalName == "Neighbors").FirstOrDefault();
            if (newNeighbors == null)
            {
                neighbor = null;
            }
            else
            {
                neighbor = new Neighbor(newNeighbors);
            }

        }
    }
    public class Neighbor
    {
        public int? iD { get; set; }
        public int? size { get; set; }
        public int? version { get; set; }
        public Items items { get; set; }

        public Neighbor(XElement neighbor)
        {
            XAttribute newId = neighbor.Attributes().Where(x => x.Name.LocalName == "Id").FirstOrDefault();
            if (newId == null)
            {
                iD = null;
            }
            else
            {
                iD = int.Parse(newId.Value);
            }
            XAttribute newSize = neighbor.Attributes().Where(x => x.Name.LocalName == "Size").FirstOrDefault();
            if (newSize == null)
            {
                size = null;
            }
            else
            {
                size = int.Parse(newSize.Value);
            }
            version = int.Parse(neighbor.Elements().Where(x => x.Name.LocalName == "_version").FirstOrDefault().Value);

            XElement childItem = neighbor.Elements().Where(x => x.Name.LocalName == "_items").FirstOrDefault();
            if (childItem != null)
            {
                items = new Items(childItem);
            }

        }
    }
}

答案 1 :(得分:0)

解决了这个问题。 问题是我没有为Universe类中的DataMember属性设置“set”方法。 这导致desialization无法设置并抛出异常。

感谢jdweng试图提供帮助。