我在序列化 Column 类型的自定义 mylist 时遇到错误,即 Microsoft.SqlServer.Management.smo.Column。我不知道如何序列化这个。
我已经序列化了一个类,如下所示:
ValidationClass validation = new ValidationClass();
XmlSerializer serializer = new XmlSerializer(typeof(ValidationClass));
TextWriter writer = new StreamWriter(@"D:\xml doc\AspxGenerator.xml");
serializer.Serialize(writer, validation);
writer.Close();
为此编写的验证类如下:
[Serializable]
public class ValidationClass
{
public bool IsNotNull { get; set; }
public bool IsEmail { get; set; }
public MyList<MyTable> MaxLimit { get; set; }
public int MinLimit { get; set; }
public bool IsValidDate { get; set; }
public SerializableDictionary<int, string> PropertyNames;// { get; set; }
public ValidationClass()
{
PropertyNames = new SerializableDictionary<int,string>();
PropertyNames.Add(0, "A");
PropertyNames.Add(1, "B");
PropertyNames.Add(2, "C");
MaxLimit = new MyList<MyTable>();
MyTable a = new MyTable();
MyTable b = new MyTable();
MaxLimit.Add(a);
MaxLimit.Add(b);
//MaxLimit = new MyList<int>();
//MaxLimit.Add(0);
//MaxLimit.Add(2);
}
}
MyList 也实现为:
[XmlRoot("dictionary")]
public class MyList<Tkey> : List<Tkey>, IXmlSerializable
{
#region IXmlSerializable Members
public System.Xml.Schema.XmlSchema GetSchema()
{
return null;
}
public void ReadXml(System.Xml.XmlReader reader)
{
XmlSerializer keySerializer = new XmlSerializer(typeof(Tkey));
bool wasEmpty = reader.IsEmptyElement;
reader.Read();
if (wasEmpty)
return;
while (reader.NodeType != System.Xml.XmlNodeType.EndElement)
{
reader.ReadStartElement("item");
reader.ReadStartElement("key");
Tkey key = (Tkey)keySerializer.Deserialize(reader);
reader.ReadEndElement();
this.Add(key);
reader.ReadEndElement();
reader.MoveToContent();
}
reader.ReadEndElement();
}
public void WriteXml(System.Xml.XmlWriter writer)
{
XmlSerializer keySerializer = new XmlSerializer(typeof(Tkey));
foreach (Tkey key in this)
{
writer.WriteStartElement("item");
writer.WriteStartElement("key");
keySerializer.Serialize(writer, key);
writer.WriteEndElement();
writer.WriteEndElement();
}
}
#endregion
}
运行此程序后,我收到一个错误:“反映类型'Microsoft.SqlServer.Management.Smo.Column'的错误。”
内部异常是:“{”要成为 XML 可序列化的,从 ICollection 继承的类型必须在其继承层次结构的所有级别上都有 Add(Microsoft.SqlServer.Management.Smo.Property) 的实现。Microsoft.SqlServer.Management.Smo.SqlPropertyCollection 未实现 Add(Microsoft.SqlServer.Management.Smo.Property)。"}"
IXmlSerilzable 的实现是完美的,没有任何问题。问题在于我使用的类型是 Column,即 Microsoft.SqlServer.Management.smo.Column。我通过将“列”类型替换为“字符串”类型来检查此代码,然后它运行良好。类列也是密封类。
我想序列化 ValidationClass。请建议如何做。
谢谢和问候, Mayur