我不确定我所遇到的错误的来源究竟是什么,所以请提供更多细节,如果有帮助的话。出于某种原因,当我尝试反序列化我的对象时,不是所有数据都返回(查看文本文件,看起来好像数据都在那里)。
以下是我认为可以解决问题的问题(这个项目有很多代码,所以很多东西都被删除了以便于阅读):
[Serializable]
public class Unit
{
public List<ModuleSlot> slotsInFront { get; set; }
//this would never be reached because the module slot has a customer xml serial object that prevents the normal xml reader from reaching this
public string some_fake_text{get;set;}
}
public class ModuleSlot
{
[XmlIgnore()]
public StreamShape moduleShape { get; set; }
[XmlElement("moduleShape")]
public ShapeSerializer xmlModuleShape
{
get
{
if (moduleShape == null)
return null;
else
{
return new ShapeSerializer(moduleShape);
}
}
set
{
moduleShape = value.getFirstShape();
}
}
}
public class ShapeSerializer : IXmlSerializable
{
public void ReadXml(System.Xml.XmlReader reader)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
System.Xml.XmlReader subReader = XmlReader.Create(reader.ReadSubtree(), settings);
subReader.ReadStartElement();
while (subReader.Depth > 0)
{
xmlStreamShape newXmlShape = new xmlStreamShape();
newXmlShape = (xmlStreamShape)new XmlSerializer(typeof(xmlStreamShape)).Deserialize(subReader);
parameters.Add(newXmlShape.getShapeFromSaved());
subReader.Read();
}
}
public void WriteXml(System.Xml.XmlWriter writer)
{
XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add("", "");
foreach (StreamShape item in parameters)
{
xmlStreamShape convertedShape = new xmlStreamShape(item);
new XmlSerializer(typeof(xmlStreamShape)).Serialize(writer, convertedShape, ns);
}
}
}
...
答案 0 :(得分:1)
在xml编写器中发现了这个问题。我放弃了形状上的序列化,所以不会在这里发布一个例子,但这里有一个方法有同样的问题但已经解决了(随意发布你用它看到的任何问题):
public void ReadXml(System.Xml.XmlReader reader)
{
//Skip whitespaces
XmlReaderSettings settings = new XmlReaderSettings();
settings.IgnoreWhitespace = true;
//Create a reader that will read the list's content
System.Xml.XmlReader subReader = XmlReader.Create(reader.ReadSubtree(), settings);
subReader.MoveToContent();
subReader.ReadStartElement();
while (subReader.Depth > 0)
{
Type type = Type.GetType(this.GetType().Namespace + "." + reader.Name);
parameters.Add((Port)new XmlSerializer(type).Deserialize(subReader));
}
reader.ReadEndElement();
}