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

  • Home
  • SEARCH
  • 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 6099673
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:17:59+00:00 2026-05-23T13:17:59+00:00

I am using two methods below to serialize/deserialize entity framework object (ver. 4.0). I

  • 0

I am using two methods below to serialize/deserialize entity framework object (ver. 4.0).
I tried several ways to accomplish this, and had no luck. Serialization works fine. I get nice xml formatted string, but when I try to deserialize I get error in XML. How is that possible?

Thanks.

    public static string SerializeObject(Object obj)
    {
        XmlSerializer ser = new XmlSerializer(obj.GetType());
        System.Text.StringBuilder sb = new System.Text.StringBuilder();
        System.IO.StringWriter writer = new System.IO.StringWriter(sb);
        ser.Serialize(writer, obj);
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(sb.ToString());
        string xml = doc.InnerXml;
        return xml;
    }
    public static object DeSerializeAnObject(string xml, Type objType)
    {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(xml);
        XmlNodeReader reader = new XmlNodeReader(doc.DocumentElement);
        XmlSerializer ser = new XmlSerializer(objType);
        object obj = ser.Deserialize(reader);
        return obj;
    }
  • 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-05-23T13:17:59+00:00Added an answer on May 23, 2026 at 1:17 pm

    I use generic methods to serialize and deserialize:

    /// <summary>
    /// Serializes an object to Xml as a string.
    /// </summary>
    /// <typeparam name="T">Datatype T.</typeparam>
    /// <param name="ToSerialize">Object of type T to be serialized.</param>
    /// <returns>Xml string of serialized type T object.</returns>
    public static string SerializeToXmlString<T>(T ToSerialize)
    {
        string xmlstream = String.Empty;
    
        using (MemoryStream memstream = new MemoryStream())
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
            XmlTextWriter xmlWriter = new XmlTextWriter(memstream, Encoding.UTF8);
    
            xmlSerializer.Serialize(xmlWriter, ToSerialize);
            xmlstream = UTF8ByteArrayToString(((MemoryStream)xmlWriter.BaseStream).ToArray());
        }
    
        return xmlstream;
    }
    
    /// <summary>
    /// Deserializes Xml string of type T.
    /// </summary>
    /// <typeparam name="T">Datatype T.</typeparam>
    /// <param name="XmlString">Input Xml string from which to read.</param>
    /// <returns>Returns rehydrated object of type T.</returns>
    public static T DeserializeXmlString<T>(string XmlString)
    {
        T tempObject = default(T);
    
        using (MemoryStream memoryStream = new MemoryStream(StringToUTF8ByteArray(XmlString)))
        {
            XmlSerializer xs = new XmlSerializer(typeof(T));
            XmlTextWriter xmlTextWriter = new XmlTextWriter(memoryStream, Encoding.UTF8);
    
            tempObject = (T)xs.Deserialize(memoryStream);
        }
    
        return tempObject;
    } 
    
    // Convert Array to String
    public static String UTF8ByteArrayToString(Byte[] ArrBytes)
    { return new UTF8Encoding().GetString(ArrBytes); }
    // Convert String to Array
    public static Byte[] StringToUTF8ByteArray(String XmlString)
    { return new UTF8Encoding().GetBytes(XmlString); }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

[UPDATE] To conclude this question, I implemented my graph using the following two methods
I wish to know all the pros and cons about using these two methods.
I have parsed XML using both of the following two methods... Parsing the XmlDocument
Consider the following .Net ASMX web service with two web methods. using System; using
Edit: Two options shown below. If you're just using the functionality that an IDisposable
I am wondering why the two methods listed below do not give the same
What's the difference between these two methods of reading an input file? 1) Using
So I am having an issue within the two methods posted below. Aside from
Objects in C++ can be created using the methods listed below(that I am aware
I am using servlet there is two method redirect and forward both are send

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.