Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8747823
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:25:16+00:00 2026-06-13T12:25:16+00:00

I am trying to Serialize a Class object and store the xml in a

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:25:17+00:00Added an answer on June 13, 2026 at 12:25 pm

    Your serialization method should look like this:

    public static string SerializeAnObject(Object item) {
        if (item == null)
            return null;
    
        var stringBuilder = new StringBuilder();
        var itemType = item.GetType();
    
        new XmlSerializer(itemType).Serialize(new StringWriter(stringBuilder), item);
    
        return stringBuilder.ToString();
    }
    

    The function above works when I test it like this:

    var test = new Flow();
    var xmlString = SerializeAnObject(test);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i am trying to XML serialize a class object but having the following problem:
I've been trying to serialize an XML (jQuery object) to string to POST it
I'm getting this error when trying to serialize a class: There was an error
I'm trying to serialize an object to XML that has a number of properties,
I'm trying to serialize into a Xml File an object which is declared as
I am trying to serialize an Exception object in C#. However, it appears that
I'm trying to serialize a class Message from a java server and send them
I'm trying to XML-serialize a class the properties of which I use to format
I'm trying to serialize a Java Dynamic proxy using Jackson library but I get
I'm trying to serialize an object as XML and have been using a little

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.