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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T03:02:59+00:00 2026-06-18T03:02:59+00:00

My data entity contains a Dictionary, but XmlSerializer does not support them out of

  • 0

My data entity contains a Dictionary, but XmlSerializer does not support them out of the box. So I decided to use DataContractSerializer. The problem is that I cannot get it to behave as I need.

I started with the following code:

public static string SerializeObject<T>(T serialisable)
{
    var serializer = new DataContractSerializer(serialisable.GetType());
    using (var writer = new StringWriter())
    using (var stm = new XmlTextWriter(writer))
    {
        serializer.WriteObject(stm, serialisable);
        return writer.ToString();
    }
}

It seemed to work fine until I noticed that if I put “\r\n” in a string, it does not get serialized to XML entities. From my experience with XmlSerializer, I knew that I can set up XmlWriterSettings with NewLineHandling = NewLineHandling.Entitize. So I converted my code to the following:

public static string SerializeObject<T>(T serialisable)
{
    var serializer = new DataContractSerializer(serialisable.GetType());
    using (var writer = new StringWriter())
    {
        using (var stm = XmlWriter.Create(writer,
            new XmlWriterSettings()
            {
                NewLineHandling = NewLineHandling.Entitize
            }))
        {
            serializer.WriteObject(stm, serialisable);
            return writer.ToString();
        }
    }
}

Now the problem is that I get an empty string. No exceptions, nothing – just an empty string.
The stm variable holds XmlWellFormedWriter. Maybe it’s not supported by DataContractSerializer?

Then I tried to enforce XmlTextWriter as follows:

public static string SerializeObject<T>(T serialisable)
{
    var serializer = new DataContractSerializer(serialisable.GetType());
    using (var writer = new StringWriter())
    using (var stm = XmlWriter.Create(new XmlTextWriter(writer),
        new XmlWriterSettings()
        {
            NewLineHandling = NewLineHandling.Entitize
        }))
    {
        serializer.WriteObject(stm, serialisable);
        return writer.ToString();
    }
}

And this gets me back to where I started – I get back XML string, but again “\r\n” string is not translated to entities.

How do I make DataContractSerializer to entitize newlines and return XML as string?

  • 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-18T03:03:01+00:00Added an answer on June 18, 2026 at 3:03 am

    It seems, the problem is mostly because of how disposing XmlWriter works – if I create it with XmlWriter.Create, it does not flush until it’s closed, so the StringWriter is empty. What’s weird – if I create it with new XmlTextWriter, it somehow flushes its contents to the StringWriter, so my initial method worked just fine.

    This time I just had to rearrange one line of code:

        public static string SerializeObject<T>(T serialisable)
        {
            var serializer = new DataContractSerializer(serialisable.GetType());
            using (var writer = new StringWriter())
            {
                using (var stm = XmlWriter.Create(writer,
                    new XmlWriterSettings()
                    {
                        NewLineHandling = NewLineHandling.Entitize,
                        Encoding = UTF8Encoding.UTF8
                    }))
                {
                    serializer.WriteObject(stm, serialisable);
                    // <- previously writer.ToString() was here and I got an empty string
                }     
    
                return writer.ToString();
            }
        }
    

    Now “\r” characters are encoded correctly as &#xD;, but “\n” are not. And encoding is still utf-16, although I set it to UTF8. I guess, that’s another issue.

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

Sidebar

Related Questions

I have this error message could not load assembly 'system.data.entity, version 4.2.0.0, culture=neutral,publickeytoken=b77a5c561934e089' or
I'm using EF Code First and Data Scaffolding and have an entity that contains
Why would my data not be saving? Db is of type System.Data.Entity.DbContext When I
I have a Core Data entity which contains dates (e.g. 2011-03-30, 2011-06-30, 2011-09-30). How
I have some data being pulled in from an Entity model. This contains attributes
I have an application that contains methods that work with data using Entity Framework
I have a Core Data entity which needs a gender property. I just need
I have an Core Data Entity with a number of attributes, which include amount(float),
I have this type of data: entity, account, month1, month2, ..., month12 abc, 2000.02,
I am trying to centralize Core Data calls for each Core Data entity into

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.