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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:23:47+00:00 2026-05-31T18:23:47+00:00

Background I serialize a very large List<string> using this code: public static string SerializeObjectToXML<T>(T

  • 0

Background

I serialize a very large List<string> using this code:

public static string SerializeObjectToXML<T>(T item)
{
    XmlSerializer xs = new XmlSerializer(typeof(T));
    using (StringWriter writer = new StringWriter())
    {
        xs.Serialize(writer, item);
        return writer.ToString();
    }
}

And deserialize it using this code:

public static T DeserializeXMLToObject<T>(string xmlText)
{
    if (string.IsNullOrEmpty(xmlText)) return default(T);
    XmlSerializer xs = new XmlSerializer(typeof(T));
    using (MemoryStream memoryStream = new MemoryStream(new UnicodeEncoding().GetBytes(xmlText.Replace((char)0x1A, ' '))))
    using (XmlTextReader xsText = new XmlTextReader(memoryStream))
    {
        xsText.Normalization = true;
        return (T)xs.Deserialize(xsText);
    }
}

But I get this exception when I deserialize it:

XMLException: There is an error in XML document (217388, 15). ‘[]’, hexadecimal value 0x1A, is an invalid character. Line 217388, position 15.

at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)

at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader)

Question

Why is the xmlText.Replace((char)0x1A, ' ') line not working, what witchery is this?

Some Constraints

  • My code is in C#, framework 4, built in VS2010 Pro.
  • I can’t view the value of xmlText in debug mode because the List<string> is too big and the watch windows just displays the Unable to evaluate the expression. Not enough storage is available to complete this operation. error message.
  • 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-31T18:23:48+00:00Added an answer on May 31, 2026 at 6:23 pm

    I think I’ve found the problem. By default, XmlSerializer will allow you to generate invalid XML.

    Given the code:

    var input = "\u001a";
    
    var writer = new StringWriter();
    var serializer = new XmlSerializer(typeof(string));
    serializer.Serialize(writer, input);
    
    Console.WriteLine(writer.ToString());
    

    The output is:

    <?xml version="1.0" encoding="utf-16"?>
    <string>&#x1A;</string>
    

    This is invalid XML. According to the XML specification, all character references must be to characters which are valid. Valid characters are:

    #x9 | #xA | #xD | [#x20-#xD7FF] | [#xE000-#xFFFD] | [#x10000-#x10FFFF]
    

    As you can see, U+001A (and all other C0/C1 control characters) are not allowed as references, since they are not valid characters.

    The error message given by the decoder is a bit misleading, and would be clearer if it said that there was an invalid character reference.

    There are several options for what you can do.

    1) Don’t let the XmlSerializer create invalid documents in the first place

    You can use an XmlWriter, which by default will not allow invalid characters:

    var input = "\u001a";
    
    var writer = new StringWriter();
    var serializer = new XmlSerializer(typeof(string));
    
    // added following line:
    var xmlWriter = XmlWriter.Create(writer);
    
    // then, write via the xmlWriter rather than writer:
    serializer.Serialize(xmlWriter, input);
    
    Console.WriteLine(writer.ToString());
    

    This will throw an exception when the serialization occurs. This will have to be handled and an appropriate error shown.

    This probably isn’t useful for you because you have data already stored with these invalid characters.

    or 2) Strip out references to this invalid character

    That is, instead of .Replace((char)0x1a, ' '), which isn’t actually replacing anything in your document at the moment, use .Replace("&#x1A;", " "). (This isn’t case-insensitive, but it is what .NET generates. A more robust solution would be to use a case-insensitive regex.)


    As an aside, XML 1.1 actually allows references to control characters, as long as they are references and not plain characters in the document. This would solve your problem apart from the fact that the .NET XmlSerializer doesn’t support version 1.1.

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

Sidebar

Related Questions

For the background to this question, see How to I serialize a large graph
Background I am writing and using a very simple CGI-based (Perl) content management tool
Background .NET 4, C#, MVC3, using JsonFx to serialize and deserialize data. Base controller
We have code like: ms = New IO.MemoryStream bin = New System.Runtime.Serialization.Formatters.Binary.BinaryFormatter bin.Serialize(ms, largeGraphOfObjects)
Background: One of the problems with using a local static variable in a function
Apologies in advance if this becomes a very long question... Background Info I have
I'm using this function, to submit form in the background, with custom messages. It
Background: At my company we are developing a bunch applications that are using the
Background I work for a large organization which has thousands of MS Access applications
Hello and thanks for looking! Background I am designing a greenfield application using .NET4

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.