如何?类对象的XML序列化 [英] How to? XML Serialization of a Class Object

查看:66
本文介绍了如何?类对象的XML序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试序列化Class对象并将xml存储在字符串中,但是每次收到异常消息生成xml文档时出错"

我要序列化的类对象属于类:

I am trying to Serialize a Class object and store the xml in a string but each time I get an exception message "There was an error generating xml document"

The class object I am trying to serialize is of class:

[XmlRoot("FlowOfTask")] 
    public class Flow
    {
        int _CurrHop = 0;

        [XmlElement("CurrentHop")]
        public int CurrentHop
        {
            get { return _CurrHop; }
            set { _CurrHop = value; }
        }
        
        int _TotalHops = 0;

        [XmlElement("TotalHops")]
        public int TotalHops
        {
            get { return _TotalHops; }
        }
        
        private List<tblTaskHop> _TaskHops;

        [System.Xml.Serialization.XmlArrayItemAttribute(ElementName = "Hop",
        IsNullable = false)]
        public List<tblTaskHop> TaskHops
        {
            get { return _TaskHops; }
        }
        public Flow()
        {

        }

        public Flow(Int64 TaskID, Int64 RoleID)
        {
            _TaskHops = HandleDB.tblTaskHopGetByTaskIDRoleID(TaskID, RoleID);
            _TotalHops = TaskHops.Count;
        }

    }



我正在使用此功能进行序列化.



I am using this function to serialize.

public static string SerializeAnObject(object item)
        {

            try
            {
                string xmlText;

                //Get the type of the object
                Type objectType = item.GetType();

                //create serializer object based on the object type
                XmlSerializer xmlSerializer = new XmlSerializer(objectType);

                //Create a memory stream handle the data
                MemoryStream memoryStream = new MemoryStream();

                //Create an XML Text writer to serialize data to
                using (XmlTextWriter xmlTextWriter =
                    new XmlTextWriter(memoryStream, Encoding.UTF8) { Formatting = Formatting.Indented })
                {

                    //convert the object to xml data
                    xmlSerializer.Serialize(xmlTextWriter, item);

                    //Get reference to memory stream
                    memoryStream = (MemoryStream)xmlTextWriter.BaseStream;

                    //Convert memory byte array into xml text
                    xmlText = new UTF8Encoding().GetString(memoryStream.ToArray());

                    //clean up memory stream
                    memoryStream.Dispose();
                    return xmlText;
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
                return null;
            }
        }





谁能帮我为什么我不能序列化该类对象?





Can anyone help me why I am not able to serialize this class object?

推荐答案

我建​​议您使用更健壮,非侵入式且易于使用的方法序列化的类型:数据合同.请参阅:
http://msdn.microsoft.com/en-us/library/ms733127.aspx [ ^ ].

在我倡导这种方法的地方,也请参阅我过去的答案:
如何在我的表单应用程序? [ ^ ],
创建属性文件... [在列表框中添加项目vb.net不使用对象和数据库 [
I would advice you to use more robust, non-intrusive and easy-to-use kind of serialization: Data Contract. Please see:
http://msdn.microsoft.com/en-us/library/ms733127.aspx[^].

Please also see my past answers where I advocate this approach:
How can I utilize XML File streamwriter and reader in my form application?[^],
Creating property files...[^].

This one is to get some idea on usage, also from CodeProject Questions & Answers:
adding items on the listbox in vb.net not using the object and database[^].

—SA


这篇关于如何?类对象的XML序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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