Bru*_*oLM 4 c# xml-serialization
我有两节课:
namespace Something
{
[Serializable]
public class Spec
{
public string Name { get; set; }
[XmlArray]
public List<Value> Values { get; set; }
}
[Serializable]
public class Value
{
public string Name { get; set; }
public short StartPosition { get; set; }
public short EndPosition { get; set; }
public Value(string name, short startPosition, short endPosition)
{
Name = name;
StartPosition = startPosition;
EndPosition = endPosition;
}
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试序列化时
var spec = new Spec();
spec.Name = "test";
spec.Values = new List<Value> { new Value("testing", 0, 2) };
var xmls = new XmlSerializer(spec.GetType());
xmls.Serialize(Console.Out, spec);
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
出现InvalidOperationException
有一个错误反映了'Something.Spec'类型
使用string
我没有任何问题的列表.我错过了一些属性吗?
本Value
类需要有一个默认的构造函数,如果你希望它是序列化.例:
public class Value
{
public string Name { get; set; }
public short StartPosition { get; set; }
public short EndPosition { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
此外,您不需要[Serializable]
XML序列化的属性,XmlSerializer类完全忽略它.