I've got an issue with which I would appreciate some assistance in its
resolution.
I've got a complex business object that used for a website.  The website
programmers need to serialize the object to viewstate.  This process works
quite well as it stands now.  Now an issue has arisen that requires
"removed" items (in one of the collection objects attached to the business
object via a property) to be serialized and then restored in order to allow
for canceling the removal (delete).

What I've run into so far is that if I implement the IXmlSerializable
interface on the collection itself, then serialize the complex business
object, the property that did serialize now won't serialize at all.  Also,
when putting a breakpoint in the IXmlSerializable interface code, it does
not break.  This has become very frustrating.

I've listed some sample code below that demonstrates this behavior.  As you
can see, the exceptions that are thrown in the serialization code are not
thrown.
Any suggestions would be appreciated.

Thanks,
Mike


   class Program {
       static void Main(string[] args) {

           Person p = new Person();
           p.ID = Guid.NewGuid();

           Document d = new Document();
           d.Description = "Test";
           d.ID = Guid.NewGuid();
           p.Documents.Add(d);

           //The documents will not be serialized!
           string xml = p.Serialize();


       }
   }
   public abstract class ItemBase {
       public Guid ID;

       /// <summary>
       /// Returns the xml respresentation of this object.
       /// </summary>
       /// <returns></returns>
       public string Serialize() {
           StringBuilder sb = new StringBuilder();
           XmlSerializer xs = new XmlSerializer(this.GetType());
           XmlSerializerNamespaces nameSpaces = new
XmlSerializerNamespaces();
           nameSpaces.Add("", "");
           XmlWriterSettings settings = new XmlWriterSettings();
           settings.Indent = true;
           settings.OmitXmlDeclaration = true;
           settings.Encoding = Encoding.UTF8;
           XmlWriter writer = XmlWriter.Create(sb, settings);
           xs.Serialize(writer, this, nameSpaces);
           return sb.ToString();
       }

   }

   public abstract class CollectionBase<ItemType> : List<ItemType>,
IXmlSerializable where ItemType : ItemBase {

       #region IXmlSerializable Members

       System.Xml.Schema.XmlSchema
System.Xml.Serialization.IXmlSerializable.GetSchema() {
           return new System.Xml.Schema.XmlSchema();
       }

       void System.Xml.Serialization.IXmlSerializable.ReadXml(
System.Xml.XmlReader reader) {
           throw new Exception("The method or operation is not
implemented.");
       }

       void System.Xml.Serialization.IXmlSerializable.WriteXml(
System.Xml.XmlWriter writer) {
           throw new Exception("The method or operation is not
implemented.");
       }

       #endregion
   }

   public class Person: ItemBase {
       public string Name;

       private DocumentCollection _documents;
       [XmlArray("Documents"), XmlArrayItem("Document")]
       public DocumentCollection Documents {
           get {
               if (_documents == null) {
                   _documents = new DocumentCollection();
               }
               return _documents;
           }
       }
   }

   public class Document : ItemBase {
       public string Description;
   }

   public class DocumentCollection : CollectionBase<Document> { }

===================================
This list is hosted by DevelopMentorĀ®  http://www.develop.com

View archives and manage your subscription(s) at http://discuss.develop.com

Reply via email to