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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:34:24+00:00 2026-05-28T03:34:24+00:00

I wrote a tool to repair some XML files (i.e., insert some attributes/values that

  • 0

I wrote a tool to repair some XML files (i.e., insert some attributes/values that were missing) using C# and Linq-to-XML. The tool loads an existing XML file into an XDocument object. Then, it parses down through the node to insert the missing data. After that, it calls XDocument.Save() to save the changes out to another directory.

All of that is just fine except for one thing: any 
 entities that are in the text in the XML file are replaced with a new line character. The entity represents a new line, of course, but I need to preserve the entity in the XML because another consumer needs it in there.

Is there any way to save the modified XDocument without losing the 
 entities?

Thank you.

  • 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-28T03:34:24+00:00Added an answer on May 28, 2026 at 3:34 am

    The 
 entities are technically called “numeric character references” in XML, and they are resolved when the original document is loaded into the XDocument. This makes your issue problematic to solve, since there is no way of distinguishing resolved whitespace entities from insignificant whitespace (typically used for formatting XML documents for plain-text viewers) after the XDocument has been loaded. Thus, the below only applies if your document does not have any insignificant whitespace.

    The System.Xml library allows one to preserve whitespace entities by setting the NewLineHandling property of the XmlWriterSettings class to Entitize. However, within text nodes, this would only entitize \r to 
, and not \n to 
.

    The easiest solution is to derive from the XmlWriter class and override its WriteString method to manually replace the whitespace characters with their numeric character entities. The WriteString method also happens to be the place where .NET entitizes characters that are not permitted to appear in text nodes, such as the syntax markers &, <, and >, which are respectively entitized to &amp;, &lt;, and &gt;.

    Since XmlWriter is abstract, we shall derive from XmlTextWriter in order to avoid having to implement all the abstract methods of the former class. Here is a quick-and-dirty implementation:

    public class EntitizingXmlWriter : XmlTextWriter
    {
        public EntitizingXmlWriter(TextWriter writer) :
            base(writer)
        { }
    
        public override void WriteString(string text)
        {
            foreach (char c in text)
            {
                switch (c)
                {
                    case '\r':
                    case '\n':
                    case '\t':
                        base.WriteCharEntity(c);
                        break;
                    default:
                        base.WriteString(c.ToString());
                        break;
                }
            }
        }
    }
    

    If intended for use in a production environment, you’d want to do away with the c.ToString() part, since it’s very inefficient. You can optimize the code by batching substrings of the original text that do not contain any of the characters you want to entitize, and feeding them together into a single base.WriteString call.

    A word of warning: The following naive implementation will not work, since the base WriteString method would replace any & characters with &amp;, thereby causing \r to be expanded to &amp;#xA;.

        public override void WriteString(string text)
        {
            text = text.Replace("\r", "&#xD;");
            text = text.Replace("\n", "&#xA;");
            text = text.Replace("\t", "&#x9;");
            base.WriteString(text);
        }
    

    Finally, to save your XDocument into a destination file or stream, just use the following snippet:

    using (var textWriter = new StreamWriter(destination))
    using (var xmlWriter = new EntitizingXmlWriter(textWriter))
        document.Save(xmlWriter);
    

    Hope this helps!

    Edit: For reference, here is an optimized version of the overridden WriteString method:

    public override void WriteString(string text)
    {
        // The start index of the next substring containing only non-entitized characters.
        int start = 0;
    
        // The index of the current character being checked.
        for (int curr = 0; curr < text.Length; ++curr)
        {
            // Check whether the current character should be entitized.
            char chr = text[curr];
            if (chr == '\r' || chr == '\n' || chr == '\t')
            {
                // Write the previous substring of non-entitized characters.
                if (start < curr)
                    base.WriteString(text.Substring(start, curr - start));
    
                // Write current character, entitized.
                base.WriteCharEntity(chr);
    
                // Next substring of non-entitized characters tentatively starts
                // immediately beyond current character.
                start = curr + 1;
            }
        }
    
        // Write the trailing substring of non-entitized characters.
        if (start < text.Length)
            base.WriteString(text.Substring(start, text.Length - start));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I wrote a simple tool to generate a DBUnit XML dataset using queries that
I would like to write a command line tool that passes some formatted text
I wrote a .net assembly using c# to perform functions that will be used
I wrote a tool for polar functions. It lists values from an input range
I have a bunch of diagrams created using a Java diagramming tool that I
I have been playing around with some debugging and wrote some C code that
I'm new in the WCF I wrote some server that work on IIS 7.5
Hello I'm trying to write some tool using code::blocks, wxWidgets and libxml2 on Windows
I wrote this function that feeds into an XSLT tool for reporting. It's incredibly
I'm trying to clean up some old code that I wrote to comply with

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.