嗨,我正在尝试序列化一个从类派生的对象数组,我一直在用c#遇到同样的错误。非常感谢您的帮助。
显然这个例子已经被缩小了,因为在现实世界中,这个帖子的形状会包含大量不同的形状。
namespace XMLInheritTests
{
class Program
{
static void Main(string[] args)
{
Shape[] a = new Shape[1] { new Square(1) };
FileStream fS = new FileStream("C:\\shape.xml",
FileMode.OpenOrCreate);
XmlSerializer xS = new XmlSerializer(a.GetType());
Console.WriteLine("writing");
try
{
xS.Serialize(fS, a);
}
catch (Exception e)
{
Console.WriteLine(e.InnerException.ToString());
Console.ReadKey();
}
fS.Close();
Console.WriteLine("Fin");
}
}
}
形状.cs
namespace XMLInheritTests
{
public abstract class Shape
{
public Shape() { }
public int area;
public int edges;
}
}
方块字.cs
namespace XMLInheritTests
{
public class Square : Shape
{
public int iSize;
public Square() { }
public Square(int size)
{
iSize = size;
edges = 4;
area = size * size;
}
}
}
错误:
System.InvalidOperationException:不应为XMLInheritTests.Square类型。使用xmlclude或SoapInclude属性指定静态未知的类型。
位于Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationWriterShapeA
非常感谢