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 3668600
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T02:08:31+00:00 2026-05-19T02:08:31+00:00

If you run this code: public class Program { public class MyClass { public

  • 0

If you run this code:

public class Program
{
    public class MyClass
    {
        public string Text { get; set; }
    }

    static MyClass myClass = new MyClass();
    static string Desktop = "C:\\Users\\Juan Luis\\Desktop\\";

    static void Main(string[] args)
    {
        myClass.Text = "\r\nhello";
        Console.WriteLine((int)myClass.Text[0]);
        Save(Desktop + "file.xml", myClass);
        myClass = Load(Desktop + "file.xml");
        Console.WriteLine((int)myClass.Text[0]);
        Console.Read();
    }

    private static MyClass Load(string fileName)
    {
        MyClass result = null;
        using (Stream stream = File.Open(fileName, FileMode.Open))
        {
            XmlSerializer xmlFormatter = new XmlSerializer(typeof(MyClass));
            result = (MyClass)xmlFormatter.Deserialize(stream);
        }
        return result;
    }

    private static void Save(string fileName, MyClass obj)
    {
        using (Stream tempFileStream = File.Create(fileName))
        {
            XmlSerializer xmlFormatter = new XmlSerializer(typeof(MyClass));
            xmlFormatter.Serialize(tempFileStream, obj);
        }
    }
}

The output will be 13, 10. The XmlSerializer is removing the carriage return. This is a problem in my case because I need to compare strings for equality in a class that gets serialized and deserialized, and this is causing two strings that are equal before serializing to be unequal after serialized. What would be the best work around?

Edit: After reading answers, this was my solution, in case it will help anyone:

public class SafeXmlSerializer : XmlSerializer
{
    public SafeXmlSerializer(Type type) : base(type) { }

    public new void Serialize(Stream stream, object o)
    {
        XmlWriterSettings ws = new XmlWriterSettings();
        ws.NewLineHandling = NewLineHandling.Entitize;

        using (XmlWriter xmlWriter = XmlWriter.Create(stream, ws))
        {
            base.Serialize(xmlWriter, o);
        }
    }
}
  • 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-19T02:08:32+00:00Added an answer on May 19, 2026 at 2:08 am

    I wouldn’t call it unreliable exactly: the XmlSerializer strips white space around text inside elements. If it didn’t do this then the meaning of XML documents would change according to how you formatted them in the IDE.

    You could consider putting the text in a CDATA section, which will preserve the contents exactly. For example, How do you serialize a string as CDATA using XmlSerializer?

    Edit: This looks to have a better explanation of where the problem lies, along with a simpler solution – How to keep XmlSerializer from killing NewLines in Strings?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am having trouble getting my class to from program.cs to run in form.cs
I have a 32 bit .NET class library having a simple public class and
I have a Breadth-First algorithm that scans a graph of nodes. I am new
I am a bit frustrated as to how to do this.., Can anyone please
Somehow when I compile this in VS 2008 it doesn't raise an error, is
My teacher in an upper level Java class on threading said something that I
I have a simple console application that is designed to read a custom Configuration
Possible Duplicate: Finally Block Not Running?? I have a question regarding finally block in
Since I added a splash screen my main form will sometimes (about 1 every
All I want to do is start a thread to listen for communication on

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.