XmlSerialization of Microsoft.SqlServer.Management.smo.Column.
-
Friday, April 20, 2012 10:18 AM
I am facing an error while serializing a a custom mylist of type Column i.e. Microsoft.SqlServer.Management.smo.Column. I don't know how to serialize this one.
I have serialized a class as follows :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();
The validation class written for this is as follows :
[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); } }Also the MyList is implemented as :
[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 }After running this I am getting an Error : "There was an error reflecting type 'Microsoft.SqlServer.Management.Smo.Column'."
Inner Exception is : "{"To be XML serializable, types which inherit from ICollection must have an implementation of Add(Microsoft.SqlServer.Management.Smo.Property) at all levels of their inheritance hierarchy. Microsoft.SqlServer.Management.Smo.SqlPropertyCollection does not implement Add(Microsoft.SqlServer.Management.Smo.Property)."}"
The implementation IXmlSerilzable is perfect and not having any problem. The problem is with the type I have used which Column i.e. Microsoft.SqlServer.Management.smo.Column. I checked this code by replacing the "Column" type with "string" type, then it worked perfectly.
I don't know how to serialize this. Also the class column is sealed class.
Please suggest how to do it.
Thanks & Regards,
Mayur Mahajan
All Replies
-
Monday, April 23, 2012 7:49 PMModerator
You can't serialize a class that cannot be serialized.
Send something else instead. Maybe just send the column name. If you need the column name and data type, then create your own struct to hold that information, and send your own struct instead.
John Saunders
WCF is Web Services. They are not two separate things.
Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
Use File->New Project to create Web Service Projects

