Click here to Skip to main content
16,019,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am facing problem in converting Object Collection to XML

anybody can help me regarding, Greatly appreciated!

I want to convert the objectCollection to XML string. I am getting the following error '{The type Site was not expected. Use the XmlInclude or SoapInclude attribute to specify types that are not known statically}'

or you can suggest me better way to doing this.

here is the code...

public abstract class BusinessObjectBase
    {
	protected Guid? _UniqueId;
        public BusinessObjectBase()
        {
_UniqueId = Guid.NewGuid();
       }
}
    }


public class Site : BusinessObjectBase
    {
        #region "Member Variables"

        private string _siteName = "";
        private string _code = "";

        #endregion

        #region "Constructors"

        public Site(string name, string code)
        {
            _siteName = name;
            _code = code;
        }

       public Site()
        {
            //nothing
        }

        #endregion

        #region "Properties"

        public string siteName
        {
            get
            {
                return _siteName;
            }
            set
            {
                _siteName = value;
            }
        }

        public string code
        {
            get
            {
                return _code;
            }
            set
            {
                _code = value;
            }
        }

        #endregion
    }


public class BusinessObjectCollection< T> : ICollection< T > where T : BusinessObjectBase
    {

        private List< BusinessObjectBase > items = new List<businessobjectbase>(); 

  #region ICollection<t> Members

        public void Add(Site item)
        {
            items.Add(item); 
        }

        public void Clear()
        {
            items.Clear();  
        }

        public bool Contains(Site item)
        {
            if (items.Contains(item))
                return true;
            else
                return false; 
        }

        public void CopyTo(Site[] array, int arrayIndex)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public int Count
        {
            get { return items.Count; }
        }

        public  IList<businessobjectbase>  GetCollection 
        {
            get { return items; }
        }

        public bool IsReadOnly
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public bool Remove(Site item)
        {
            bool isremoved=false;
            isremoved = items.Remove(item);
            return isremoved; 

        }


        #endregion

        #region IEnumerable<t> Members

        public IEnumerator<t> GetEnumerator()
        {
            return new BusinessObjectEnumerator<t>(this);
        }

        #endregion</t></t></t></businessobjectbase></t></businessobjectbase>




public class BusinessObjectManager
    {
        int _MaxId;

        public void Save(IList<businessobjectbase> colObject)
        {
string xmlObject;
                xmlObject = GetobjAsXmlDocument(colObject);
			Save(xmlObject);
                           }
private static string GetobjAsXmlDocument(object Entity)
        {
            XmlDocument xmlDoc = new XmlDocument();
            XmlSerializer xmlSerializer = new XmlSerializer(Entity.GetType());//
            using (MemoryStream xmlStream = new MemoryStream())
            {
                xmlSerializer.Serialize(xmlStream, Entity);
                xmlStream.Position = 0;
                xmlDoc.Load(xmlStream);
                return xmlDoc.InnerXml;
            }
        }
               }</businessobjectbase>


Program.cs

      BusinessObjectCollection<site> Sitecollection = new BusinessObjectCollection<site>();

Site Site1 = new Site("Amit", "Raj");
Site Site2 = new Site("James", "Dan");

Sitecollection.Add(Site1);
Sitecollection.Add(Site2);

            BusinessObjectManager objManager = new BusinessObjectManager();
            objManager.Save(Site_collection.GetCollection);</site></site>
Posted

You could add a property to your object that returns a XElement which contains the properties you're interested in. I think it's a lot more straightforward than serializing. I do it all the time.
 
Share this answer
 
The only thing that I have noticed on a quick glance at your code is that none of your classes are decorated with the SerializableAttribute which they should be for successful serialization.
SerializableAttribute - MSDN[^]
Introduction to Serialization[^]

You might also find Serializing with .Net 2.0 Generics[^] interesting.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900