in

CodeSmith Community

Your Code. Your Way. Faster!

Collection, XML Serialization and IComponent/ISite

Last post 09-11-2006 3:19 PM by rafa.alm. 1 replies.
Page 1 of 1 (2 items)
Sort Posts: Previous Next
  • 07-19-2006 4:37 PM

    Collection, XML Serialization and IComponent/ISite

    Save my sanity! I'm working on a collection class that I cannot get to XML Serialize due to a IComponent/ISite issue.

    Error: Cannot serialize member System.ComponentModel.Component.Site of type System.ComponentModel.ISite because it is an interface.

    There are many many posts thrashing about on this issue without a decent answer. The issue is related to using the IComponent interface. I have tried using the [XMLIgnoreAttribute()] attribute all over in the "Implement the IComponent members" region to no avail. Some have suggested that you cannot XML Serialize anything using the IComponent interface. What?! Could that really be true? Most other related posts seem to suggest some variation of:

            [XMLIgnore]
            ISite _site = null;


    Note: This object CAN store in the ViewState, which I believe also uses serialization. (But not XML.)

    Here is my BizObjects code:
    ------------------------------------------------------------------
    using System;
    using System.Text;
    using System.Collections;
    using System.ComponentModel;
    using System.Runtime.InteropServices;
    using System.Runtime.Serialization;
    using System.Security.Permissions;
    using System.Xml.Serialization;

    namespace LbRb.BusinessEntity
    {
        /// <summary>
        /// Requests for change or creation of users or participants in the organization are encapsulated in the Request document. If approved the data in the request is moved to a <see cref="Person"/>Person</see> document.
        /// </summary>
        [Serializable()]
        [System.ComponentModel.DesignerCategory("Code")]
        public sealed class Request : Component, ISerializable
        {
            SerializationInfo info;
            StreamingContext context;

            #region Member Variables
            private Boolean isNew = true;
            private Boolean isDirty = false;
            private Guid requestId = Guid.Empty; // (PK)
            private String requesterId = String.Empty;
            private String requesterIdOld = String.Empty;
            private String requesterEmail = String.Empty;
            private String requesterEmailOld = String.Empty;
            private Guid positionId = Guid.Empty; // (FK)
            private Guid positionIdOld = Guid.Empty;
            private String position = String.Empty;
            private Guid areaId = Guid.Empty; // (FK)
            private Guid areaIdOld = Guid.Empty;
            private String area = String.Empty;
            private Guid sectionId = Guid.Empty; // (FK)
            private Guid sectionIdOld = Guid.Empty;
            private String section = String.Empty;
            private String idNumber = String.Empty;
            private String idNumberOld = String.Empty;
            private String nameLast = String.Empty;
            private String nameLastOld = String.Empty;
            private String nameFirst = String.Empty;
            private String nameFirstOld = String.Empty;
            private String nameMiddle = String.Empty;
            private String nameMiddleOld = String.Empty;
            private String nameFull = String.Empty;
            private String phone = String.Empty;
            private String phoneOld = String.Empty;
            private String email = String.Empty;
            private String emailOld = String.Empty;
            private Guid passwordTypeId = Guid.Empty; // (FK)
            private Guid passwordTypeIdOld = Guid.Empty;
            private String passwordType = String.Empty;
            private String password = String.Empty;
            private String passwordOld = String.Empty;
            private String notes = String.Empty;
            private String notesOld = String.Empty;
            private String positionCapacity = String.Empty;
            private String managerFullName = String.Empty;
            private String supervisorFullName = String.Empty;
            private String editedBy = String.Empty;
            private DateTime editedDate = DateTime.MinValue;
            private Byte[] rowversion = new ByteMusic [8];
            #endregion

            #region Constructors
            /// <summary>Initializes a new instance of the <see cref="Request">Request</see> object.</summary>
            /// <returns>A new instance of the <see cref="Request">Request</see> object.</returns>
            public Request()
            {
            }
            /// <summary>Initializes a new instance of the <see cref="Request">Request</see> object that is serializable and uses the specified System.Runtime.Serialization.SerializationInfo and System.Runtime.Serialization.StreamingContext.</summary>
            /// <returns>A new instance of the <see cref="Request">Request</see> object.</returns>
            private Request(SerializationInfo info, StreamingContext context)
            {
                RequestId = (Guid) info.GetValue("requestId", typeof(Guid));
                RequesterId = (String) info.GetValue("requesterId", typeof(String));
                RequesterIdOld = (String) info.GetValue("requesterIdOld", typeof(String));
                RequesterEmail = (String) info.GetValue("requesterEmail", typeof(String));
                RequesterEmailOld = (String) info.GetValue("requesterEmailOld", typeof(String));
                PositionId = (Guid) info.GetValue("positionId", typeof(Guid)); // (FK)
                PositionIdOld = (Guid) info.GetValue("positionIdOld", typeof(Guid)); // (FK)
                Position = (String) info.GetValue("position", typeof(String)); // (FK)
                AreaId = (Guid) info.GetValue("areaId", typeof(Guid)); // (FK)
                AreaIdOld = (Guid) info.GetValue("areaIdOld", typeof(Guid));
                Area = (String) info.GetValue("area", typeof(String));
                SectionId = (Guid) info.GetValue("sectionId", typeof(Guid)); // (FK)
                SectionIdOld = (Guid) info.GetValue("sectionIdOld", typeof(Guid));
                Section = (String) info.GetValue("section", typeof(String));
                IdNumber = (String) info.GetValue("idNumber", typeof(String));
                IdNumberOld = (String) info.GetValue("idNumberOld", typeof(String));
                NameLast = (String) info.GetValue("nameLast", typeof(String));
                NameLastOld = (String) info.GetValue("nameLastOld", typeof(String));
                NameFirst = (String) info.GetValue("nameFirst", typeof(String));
                NameFirstOld = (String) info.GetValue("nameFirstOld", typeof(String));
                NameMiddle = (String) info.GetValue("nameMiddle", typeof(String));
                NameMiddleOld = (String) info.GetValue("nameMiddleOld", typeof(String));
                Phone = (String) info.GetValue("phone", typeof(String));
                PhoneOld = (String) info.GetValue("phoneOld", typeof(String));
                Email = (String) info.GetValue("email", typeof(String));
                EmailOld = (String) info.GetValue("emailOld", typeof(String));
                PasswordTypeId = (Guid) info.GetValue("passwordTypeId", typeof(Guid)); // (FK)
                PasswordTypeIdOld = (Guid) info.GetValue("passwordTypeIdOld", typeof(Guid));
                PasswordType = (String) info.GetValue("passwordType", typeof(String));
                Password = (String) info.GetValue("password", typeof(String));
                PasswordOld = (String) info.GetValue("passwordOld", typeof(String));
                Notes = (String) info.GetValue("notes", typeof(String));
                NotesOld = (String) info.GetValue("notesOld", typeof(String));
                PositionCapacity = (String) info.GetValue("positionCapacity", typeof(String));
                ManagerFullName = (String) info.GetValue("managerFullName", typeof(String));
                SupervisorFullName = (String) info.GetValue("supervisorFullName", typeof(String));
                EditedBy = (String) info.GetValue("editedBy", typeof(String));
                EditedDate = (DateTime) info.GetDateTime("editedDate");
                Rowversion = (Byte[]) info.GetValue("rowversion", typeof(Byte[]));
                IsNew = (Boolean) info.GetValue("isNew", typeof(Boolean));
                IsDirty = (Boolean) info.GetValue("isDirty", typeof(Boolean));
            }
            /// <summary>Initializes a new instance of the <see cref="Request">Request</see> object with the given parameters.</summary>
            /// <returns>A new instance of the <see cref="Request">Request</see> object.</returns>
            public Request (Guid newRequestId, String newRequesterId, String newRequesterEmail, Guid newPositionId, String newPosition, Guid newAreaId, String newArea, Guid newSectionId, String newSection, String newIdNumber, String newNameLast, String newNameFirst, String newNameMiddle, String newNameFull, String newPhone, String newEmail, Guid newPasswordTypeId, String newPasswordType, String newPassword, String newNotes, String newPositionCapacity, String newManagerFullName, String newSupervisorFullName, String newEditedBy, DateTime newEditedDate, Byte[] newRowversion)
            {
                this.requestId = newRequestId; // (PK)
                this.requesterId = newRequesterId; // (PK)
                this.requesterIdOld = newRequesterId; // (PK)
                this.requesterEmail = newRequesterEmail; // (PK)
                this.requesterEmailOld = newRequesterEmail; // (PK)
                this.positionId = newPositionId; // (FK)
                this.positionIdOld = newPositionId;
                this.position = newPosition;
                this.areaId = newAreaId; // (FK)
                this.areaIdOld = newAreaId;
                this.area = newArea;
                this.sectionId = newSectionId; // (FK)
                this.sectionIdOld = newSectionId;
                this.section = newSection;
                this.idNumber = newIdNumber;
                this.idNumberOld = newIdNumber;
                this.nameLast = newNameLast;
                this.nameLastOld = newNameLast;
                this.nameFirst = newNameFirst;
                this.nameFirstOld = newNameFirst;
                this.nameMiddle = newNameMiddle;
                this.nameMiddleOld = newNameMiddle;
                this.nameFull = newNameFull;
                this.phone = newPhone;
                this.phoneOld = newPhone;
                this.email = newEmail;
                this.emailOld = newEmail;
                this.passwordTypeId = newPasswordTypeId; // (FK)
                this.passwordTypeIdOld = newPasswordTypeId;
                this.passwordType = newPasswordType;
                this.password = newPassword;
                this.passwordOld = newPassword;
                this.notes = newNotes;
                this.notesOld = newNotes;
                this.positionCapacity = newPositionCapacity;
                this.managerFullName = newManagerFullName;
                this.supervisorFullName = newSupervisorFullName;
                this.editedBy = newEditedBy;
                this.editedDate = newEditedDate;
                this.rowversion = newRowversion;
            }
            #endregion

            #region Properties
            public Boolean IsNew
            {
                get { return isNew; }
                set { isNew = value; }
            }

            public Boolean IsDirty
            {
                get { return isDirty; }
                set { isDirty = value; }
            }

            public Guid RequestId // (PK)
            {
                get { return requestId; }
                set { requestId = value; }
            }

            public String RequesterId
            {
                get { return requesterId; }
                set
                {
                    if ( requesterId != value)
                    {
                        this.isDirty = true;
                        requesterId = value;
                    }
                }
            }
            public String RequesterIdOld
            {
                get { return requesterIdOld; }
                set
                {
                    requesterIdOld = value;
                }
            }
            public String RequesterEmail
            {
                get { return requesterEmail; }
                set
                {
                    if ( requesterEmail != value)
                    {
                        this.isDirty = true;
                        requesterEmail = value;
                    }
                }
            }
            public String RequesterEmailOld
            {
                get { return requesterEmailOld; }
                set
                {
                    requesterEmailOld = value;
                }
            }
            public Guid PositionId // (FK)
            {
                get { return positionId; }
                set
                {
                    if ( positionId != value)
                    {
                        this.isDirty = true;
                        positionId = value;
                    }
                }
            }

            public Guid PositionIdOld
            {
                get { return positionIdOld; }
                set { positionIdOld = value; }
            }

            public String Position
            {
                get { return position; }
                set { position = value; }
            }

            public Guid AreaId // (FK)
            {
                get { return areaId; }
                set
                {
                    if ( areaId != value)
                    {
                        this.isDirty = true;
                        areaId = value;
                    }
                }
            }

            public Guid AreaIdOld
            {
                get { return areaIdOld; }
                set { areaIdOld = value; }
            }

            public String Area
            {
                get { return area; }
                set { area = value; }
            }

            public Guid SectionId // (FK)
            {
                get { return sectionId; }
                set
                {
                    if ( sectionId != value)
                    {
                        this.isDirty = true;
                        sectionId = value;
                    }
                }
            }

            public Guid SectionIdOld
            {
                get { return sectionIdOld; }
                set { sectionIdOld = value; }
            }

            public String Section
            {
                get { return section; }
                set { section = value; }
            }

            public String IdNumber
            {
                get { return idNumber; }
                set
                {
                    if ( idNumber != value)
                    {
                        this.isDirty = true;
                        idNumber = value;
                    }
                }
            }

            public String IdNumberOld
            {
                get { return idNumberOld; }
                set { idNumberOld = value; }
            }

            public String NameLast
            {
                get { return nameLast; }
                set
                {
                    if ( nameLast != value)
                    {
                        this.isDirty = true;
                        nameLast = value;
                    }
                }
            }

            public String NameLastOld
            {
                get { return nameLastOld; }
                set { nameLastOld = value; }
            }

            public String NameFirst
            {
                get { return nameFirst; }
                set
                {
                    if ( nameFirst != value)
                    {
                        this.isDirty = true;
                        nameFirst = value;
                    }
                }
            }

            public String NameFirstOld
            {
                get { return nameFirstOld; }
                set { nameFirstOld = value; }
            }

            public String NameMiddle
            {
                get { return nameMiddle; }
                set
                {
                    if ( nameMiddle != value)
                    {
                        this.isDirty = true;
                        nameMiddle = value;
                    }
                }
            }

            public String NameMiddleOld
            {
                get { return nameMiddleOld; }
                set { nameMiddleOld = value; }
            }

            public String NameFull
            {
                get { return nameFull; }
                set { nameFull = value; }
            }

            public String Phone
            {
                get { return phone; }
                set
                {
                    if ( phone != value)
                    {
                        this.isDirty = true;
                        phone = value;
                    }
                }
            }

            public String PhoneOld
            {
                get { return phoneOld; }
                set { phoneOld = value; }
            }

            public String Email
            {
                get { return email; }
                set
                {
                    if ( email != value)
                    {
                        this.isDirty = true;
                        email = value;
                    }
                }
            }

            public String EmailOld
            {
                get { return emailOld; }
                set { emailOld = value; }
            }

            public Guid PasswordTypeId // (FK)
            {
                get { return passwordTypeId; }
                set
                {
                    if ( passwordTypeId != value)
                    {
                        this.isDirty = true;
                        passwordTypeId = value;
                    }
                }
            }

            public Guid PasswordTypeIdOld
            {
                get { return passwordTypeIdOld; }
                set { passwordTypeIdOld = value; }
            }

            public String PasswordType
            {
                get { return passwordType; }
                set { passwordType = value; }
            }

            public String Password
            {
                get { return password; }
                set
                {
                    if ( password != value)
                    {
                        this.isDirty = true;
                        password = value;
                    }
                }
            }

            public String PasswordOld
            {
                get { return passwordOld; }
                set { passwordOld = value; }
            }

            public String Notes
            {
                get { return notes; }
                set
                {
                    if ( notes != value)
                    {
                        this.isDirty = true;
                        notes = value;
                    }
                }
            }

            public String NotesOld
            {
                get { return notesOld; }
                set { notesOld = value; }
            }

            public String PositionCapacity
            {
                get { return positionCapacity; }
                set { positionCapacity = value; }
            }

            public String ManagerFullName
            {
                get { return managerFullName; }
                set { managerFullName = value; }
            }

            public String SupervisorFullName
            {
                get { return supervisorFullName; }
                set { supervisorFullName = value; }
            }

            public String EditedBy
            {
                get { return editedBy; }
                set { editedBy = value; }
            }

            public DateTime EditedDate
            {
                get { return editedDate; }
                set { editedDate = value; }
            }

            public Byte[] Rowversion
            {
                get { return rowversion; }
                set { rowversion = value; }
            }

            #endregion

            #region ISerializable
            /// <summary>Implimentation of ISerializable.GetObjectData</summary>
            [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter=true)]
            public void GetObjectData(SerializationInfo info, StreamingContext context)
            {
                this.info = info;
                this.context = context;
                info.AddValue("isNew", IsNew);
                info.AddValue("isDirty", IsDirty);
                info.AddValue("requestId", RequestId);
                info.AddValue("requesterId", RequesterId);
                info.AddValue("requesterIdOld", RequesterIdOld);
                info.AddValue("requesterEmail", RequesterEmail);
                info.AddValue("requesterEmailOld", RequesterEmailOld);
                info.AddValue("positionId", PositionId); // (FK)
                info.AddValue("positionIdOld", PositionIdOld); // (FK)
                info.AddValue("position", Position); // (FK)
                info.AddValue("areaId", AreaId); // (FK)
                info.AddValue("areaIdOld", AreaIdOld); // (FK)
                info.AddValue("area", Area); // (FK)
                info.AddValue("sectionId", SectionId); // (FK)
                info.AddValue("sectionIdOld", SectionIdOld); // (FK)
                info.AddValue("section", Section); // (FK)
                info.AddValue("idNumber", IdNumber);
                info.AddValue("idNumberOld", IdNumberOld);
                info.AddValue("nameLast", NameLast);
                info.AddValue("nameLastOld", NameLastOld);
                info.AddValue("nameFirst", NameFirst);
                info.AddValue("nameFirstOld", NameFirstOld);
                info.AddValue("nameMiddle", NameMiddle);
                info.AddValue("nameMiddleOld", NameMiddleOld);
                info.AddValue("phone", Phone);
                info.AddValue("phoneOld", PhoneOld);
                info.AddValue("email", Email);
                info.AddValue("emailOld", EmailOld);
                info.AddValue("passwordTypeId", PasswordTypeId); // (FK)
                info.AddValue("passwordTypeIdOld", PasswordTypeIdOld); // (FK)
                info.AddValue("passwordType", PasswordType); // (FK)
                info.AddValue("password", Password);
                info.AddValue("passwordOld", PasswordOld);
                info.AddValue("notes", Notes);
                info.AddValue("notesOld", NotesOld);
                info.AddValue("positionCapacity", PositionCapacity);
                info.AddValue("managerFullName", ManagerFullName);
                info.AddValue("supervisorFullName", SupervisorFullName);
                info.AddValue("editedBy", EditedBy);
                info.AddValue("editedDate", EditedDate);
                info.AddValue("rowversion", Rowversion);
            }
            #endregion

        }
        /// <summary>
        /// A collection of <see cref="Request">Request</see> objects.
        /// </summary>
        [Serializable()]
        [System.ComponentModel.DesignerCategory("Code")]
        public sealed class RequestCollection : CollectionBase, IComponent
        {

            #region Constructors
            /// <summary>Initializes a new instance of the <see cref="RequestCollection">RequestCollection</see> class.</summary>
            public RequestCollection()
            {
            }
            /// <summary>Initializes a new instance of the <see cref="RequestCollection">RequestCollection</see> class containing the specified array of <see cref="Request">Request</see> objects.</summary>
            /// <param name="value">An array of <see cref="Request">Request</see> objects with which to initialize the collection. </param>
            public RequestCollection(Request[] value)
            {
                AddRange(value);
            }
            /// <summary>Initializes a new instance of the <see cref="RequestCollection">RequestCollection</see> class containing the elements of the specified source collection.</summary>
            /// <param name="value">A <see cref="RequestCollection">RequestCollection</see> with which to initialize the collection.</param>
            public RequestCollection(RequestCollection value)
            {
                AddRange(value);
            }
            #endregion

            #region ICollection
            /// <summary>Type-safe version of ICollection.CopyTo</summary>
            public void CopyTo(Request[] array, int index) {List.CopyTo(array, index);}
            #endregion

            #region IList
            /// <summary>Gets the <see cref="Request">Request</see> at the specified index in the collection.</summary>
            public Request this[int index]
            {
                get {return ((Request)(List[index]));}
            }
            /// <summary>Type-safe version of IList.Add</summary>
            public int Add(Request value) {return List.Add(value);}
            /// <summary>Type-safe version of IList.AddRange</summary>
            public void AddRange(Request[] value)
            {
                for (int i = 0; (i < value.Length); i = (i + 1))
                    Add(valueIdea [I]);
            }
                 
            /// <summary>Type-safe version of IList.AddRange</summary>
            public void AddRange(RequestCollection value)
            {
                for (int i = 0; (i < value.Count); i = (i + 1))
                    Add(valueIdea [I]);
            }
                 
            /// <summary>Type-safe version of IList.Insert</summary>
            public void Insert(int index, Request value) {List.Insert(index, value);}
            /// <summary>Type-safe version of IList.Remove</summary>
            public void Remove(Request value) {List.Remove(value);}
            /// <summary>Type-safe version of IList.Contains</summary>
            public Boolean Contains(Request value) {return List.Contains(value);}
            /// <summary>Type-safe version of IList.IndexOf</summary>
            public int IndexOf(Request value) {return List.IndexOf(value);}
            #endregion

            #region IEnumerable
            ///<summary>Type-safe version of IEnumerable.GetEnumerator</summary>
            public new RequestEnumerator GetEnumerator()
            {
                return new RequestEnumerator(this);
            }
            #endregion

            #region IEnumerator
            ///<summary>Type-safe nested iterator class</summary>
            public class RequestEnumerator : IEnumerator
            {           
                private IEnumerator baseEnumerator;           
                 
                public RequestEnumerator(RequestCollection mappings)
                {
                    IEnumerable temp = ((IEnumerable)(mappings));
                    baseEnumerator = temp.GetEnumerator();
                }
                 
                ///<summary>Type-safe iterator Current method </summary>
                public Request Current
                {
                    get {return ((Request)(baseEnumerator.Current));}
                }
                
                ///<summary>Type-unsafe IEnumerator.Current</summary>
                object IEnumerator.Current
                {
                    get {return baseEnumerator.Current;}
                }
                 
                public Boolean MoveNext() {return baseEnumerator.MoveNext();}           
                Boolean IEnumerator.MoveNext() {return baseEnumerator.MoveNext();}
                 
                public void Reset() {baseEnumerator.Reset();}           
                void IEnumerator.Reset() {baseEnumerator.Reset();}
            }
            #endregion

            #region IComponent
            public event System.EventHandler Disposed;

            ///<summary>Implement the IComponent members</summary>
            [NonSerialized()]
            ISite _site = null;

            ISite IComponent.Site
            {
                get { return _site;  }
                set { _site = value; }
            }


            ///<summary>Implement the IDisposable members (IComponent inherits from IDisposable)</summary>
            void IDisposable.Dispose()
            {
                if (Disposed != null)
                    Disposed(this, EventArgs.Empty);
            }
            #endregion

        }
    }

    Here is a snippet of the Web Service that I'm using to get the XML:
    -------------------------------------------------------------------

            [WebMethod]
            public XmlDocument GetAllRequests()
            {
                XmlDocument doc = new XmlDocument();
                RequestCollection requestCollection = RequestBusinessProcess.LoadCollection();
                MemoryStream memStream = new MemoryStream();
                XmlSerializer serializer = new XmlSerializer(typeof(RequestCollection));
                serializer.Serialize(memStream, requestCollection);
                doc.LoadXml(memStream.ToString());
                return doc;
            }

    • Post Points: 35
  • 09-11-2006 3:19 PM In reply to

    • rafa.alm
    • Not Ranked
    • Joined on 09-11-2006
    • Posts 3
    • Points 15

    Re: Collection, XML Serialization and IComponent/ISite

    Well, I have a problem with IComponent too... how is your importance at the code? It's necessary?
    • Post Points: 5
Page 1 of 1 (2 items)
Copyright © 2008 CodeSmith Tools, LLC
Powered by Community Server (Commercial Edition), by Telligent Systems