首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用ReadObject of NetDataContractSerializer时错误“值不能为空”

使用ReadObject of NetDataContractSerializer时错误“值不能为空”
EN

Stack Overflow用户
提问于 2015-06-11 02:53:34
回答 2查看 369关注 0票数 0

我试图使用NetDataContractSerializer来反序列化由NetDataContractSerializer序列化的XML。

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

这是反序列化部分。

代码语言:javascript
复制
using (FileStream reader = new FileStream(filePath,FileMode.Open))
            {
                NetDataContractSerializer ser = new NetDataContractSerializer();
                Universe loadedUniverse = (Universe)ser.ReadObject(reader);
            }

这是我得到的例外

值不能为空。参数:来源

我试着在内心深处调试它,但不明白到底哪里出了问题。FileStream似乎正确地访问了文件的内容。

也尝试使用XmlReader而不是FileStream,得到了同样的错误。使用XmlReader,我看到它也很好地获得了序列化类的类型,所以我排除了没有找到该文件或类似的情况。

这是代码的序列化部分。

代码语言:javascript
复制
NetDataContractSerializer ser =
            new NetDataContractSerializer();

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

以下是xml文件 (抱歉,找不到在线的XML共享工具)

我处理这件事的方式有什么问题吗?将感谢您的帮助!

EN

回答 2

Stack Overflow用户

发布于 2015-06-11 08:44:44

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

代码语言:javascript
复制
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
EN

Stack Overflow用户

发布于 2015-06-12 01:20:44

解决了这个问题。问题是,对于Universe类中的DataMember属性,我没有"set“方法。这导致期望无法设置它并抛出异常。

谢谢jdweng的帮助。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30770758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档