views:

36

answers:

1

Hi everyone,

I'm using XmlWriter to save an XmlDocument in .Net. However all the elements that have InnerText are written with surrounding carriage return characters.

I tried using XmlWriterSettings to avoid the writing of those characters, but no luck yet.

Here it is a piece of code used:

        XmlDocument outXml = new XmlDocument();
        outXml.AppendChild .......
        XmlWriterSettings sets = new XmlWriterSettings();
        sets.Encoding = encoding;
        sets.Indent = true;
        XmlWriter xwriter = XmlWriter.Create(file, sets);
        outXml.Save(xwriter);
        xwriter.Close();

The xml output is like:

  <String Id="msierrXmlFileFailedSave" Overridable="yes">
    Fehler beim Speichern der Einstellungen-Datei.
  </String>

The xml output needed should be like:

  <String Id="msierrXmlFileFailedSave" Overridable="yes">Fehler beim Speichern der Einstellungen-Datei.</String>

Is there a way to avoid the writing of those carriage returns inside the elements?

PD: I saw an opposite question about this issue, but the solution does not apply to this case.

Thanks in advance.

Allan.c

+1  A: 

You will want to set the XmlDocument.PreserveWhitespace property before calling Save. Another option is to use CData sections instead of inner text where the text content of the elements needs to be preserved exactly.

From the documentation:

If PreserveWhitespace is true before Load or LoadXml is called, white space nodes are preserved; otherwise, if this property is false, significant white space is preserved, white space is not.

If PreserveWhitespace is true before Save is called, white space in the document is preserved in the output; otherwise, if this property is false, XmlDocument auto-indents the output.

Josh Einstein
Excellent, It fixed the problem.
Allan.C