Click here to Skip to main content
15,029,039 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:
C#
[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.

C#
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?
Posted
Comments
Tarakeshwar Reddy 29-Oct-12 15:31pm
   
What is tblTaskHop? Is that a serializable class?

1 solution

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
   

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