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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T20:16:49+00:00 2026-05-24T20:16:49+00:00

Given this code (C#, .NET 3.5 SP1): var doc = new XmlDocument(); doc.LoadXml(<?xml version=\1.0\?><root>

  • 0

Given this code (C#, .NET 3.5 SP1):

var doc = new XmlDocument();
doc.LoadXml("<?xml version=\"1.0\"?><root>"
    + "<value xml:space=\"preserve\">"
    + "<item>content</item>"
    + "<item>content</item>"
    + "</value></root>");

var text = new StringWriter();
var settings = new XmlWriterSettings() { Indent = true, CloseOutput = true };
using (var writer = XmlWriter.Create(text, settings))
{
    doc.DocumentElement.WriteTo(writer);
}

var xml = text.GetStringBuilder().ToString();
Assert.AreEqual("<?xml version=\"1.0\" encoding=\"utf-16\"?>\r\n<root>\r\n"
    + "  <value xml:space=\"preserve\"><item>content</item>"
    + "<item>content</item></value>\r\n</root>", xml);

The assertion fails because the XmlWriter is inserting a newline and indent around the <item> elements, which would seem to contradict the xml:space="preserve" attribute.

I am trying to take input with no whitespace (or only significant whitespace, and already loaded into an XmlDocument) and pretty-print it without adding any whitespace inside elements marked to preserve whitespace (for obvious reasons).

Is this a bug or am I doing something wrong? Is there a better way to achieve what I’m trying to do?

Edit: I should probably add that I do have to use an XmlWriter with Indent=true on the output side. In the “real” code, this is being passed in from outside of my code.

  • 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-24T20:16:50+00:00Added an answer on May 24, 2026 at 8:16 pm

    Ok, I’ve found a workaround.

    It turns out that XmlWriter does the correct thing if there actually is any whitespace within the xml:space="preserve" block — it’s only when there isn’t any that it screws up and adds some. And conveniently, this also works if there are some whitespace nodes, even if they’re empty. So the trick that I’ve come up with is to decorate the document with extra 0-length whitespace in the appropriate places before trying to write it out. The result is exactly what I want: pretty printing everywhere except where whitespace is significant.

    The workaround is to change the inner block to:

    PreserveWhitespace(doc.DocumentElement);
    doc.DocumentElement.WriteTo(writer);
    

    …

    private static void PreserveWhitespace(XmlElement root)
    {
        var nsmgr = new XmlNamespaceManager(root.OwnerDocument.NameTable);
        foreach (var element in root.SelectNodes("//*[@xml:space='preserve']", nsmgr)
            .OfType<XmlElement>())
        {
            if (element.HasChildNodes && !(element.FirstChild is XmlSignificantWhitespace))
            {
                var whitespace = element.OwnerDocument.CreateSignificantWhitespace("");
                element.InsertBefore(whitespace, element.FirstChild);
            }
        }
    }
    

    I’m still thinking that this behaviour of XmlWriter is a bug, though.

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

Sidebar

Related Questions

Given this powershell code: $drivers = New-Object 'System.Collections.Generic.Dictionary[String,String]' $drivers.Add(nitrous,vx) $drivers.Add(directx,vd) $drivers.Add(openGL,vo) Is it possible
I was given this code a while back. I finally got around to testing
Given this line of code in C: printf(%3.0f\t%6.1f\n, fahr, ( (5.0/9.0) * (fahr-32) )
Given this piece of code: (void)someFunction(void) { int array[] = {1,2,3,4,5,6,7,8,9,10}; } Where are
There's something very unsatisfactory about this code: /* Given a command string in which
I'm using this code, to make a request to a given URL: private static
This code doesn't work: URL url = new URL( xmlPath ); InputSource input =
I have the following code (VB.NET) which is designed to determine if a given
Given this stack trace snippet Caused by: java.net.SocketException: Software caused connection abort: socket write
If I run this code - var html= '<html><head></head><body><div class=bar></div></body></html>'; console.log($(html).find('div')); I get no

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.