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()
{
}
#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>