如何将BusinessObjectCollection转换为XML字符串 [英] How to convert BusinessObjectCollection to XML String

查看:102
本文介绍了如何将BusinessObjectCollection转换为XML字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将对象集合转换为XML时遇到问题

任何人都可以帮助我,非常感谢!

我想将objectCollection转换为XML字符串.我收到以下错误''{{网站类型不是预期的.使用XmlInclude或SoapInclude属性可以指定静态未知的类型}"

或者您可以建议我更好的方法.

这是代码...

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



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>

推荐答案

快速浏览一下您的代码,我发现的唯一一件事是,您的所有类都没有用SerializableAttribute修饰,而应该使用SerializableAttribute修饰它们用于成功的序列化.
SerializableAttribute-MSDN [序列化简介 [使用.Net 2.0泛型进行序列化 [ ^ ]有趣.
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.


您可以在对象中添加一个属性,该属性返回一个XElement,其中包含您感兴趣的属性.我认为它比序列化要简单得多.我一直都在做.
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.


这篇关于如何将BusinessObjectCollection转换为XML字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆