错误“值不能为空”时,使用 NetDataContract Serializer 的 ReadObject
本文关键字:使用 NetDataContract Serializer ReadObject 不能 值不能为空 错误 | 更新日期: 2023-09-27 17:55:34
我正在尝试使用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);
}
这是我得到的例外
值不能为空。参数:源
我试图在内心深处调试它,但不明白那里到底出了什么问题。似乎文件流正在正确访问文件的内容。
还尝试使用XmlReader而不是FileStream,得到同样的错误。使用 XmlReader,我看到它也获得了序列化类的类型,所以我排除了找不到该文件或类似的东西。
这是代码的序列化部分
NetDataContractSerializer ser =
new NetDataContractSerializer();
using (FileStream file = new FileStream(target, FileMode.Create))
{
ser.WriteObject(file, universe);
}
这是 xml 文件(抱歉找不到在线 XML 共享工具)
我试图解决这个问题的方式有什么问题吗?将感谢您的帮助!
尝试下面的代码。 我将 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);
}
}
}
}
解决了这个问题。问题是我没有 Universe 类中 DataMember 属性的"set"方法。这导致去唾液化无法设置它并引发异常。
感谢jdweng的帮助。