开发者

Avoid the carriage return when writing InnerText in xml elements using System.Xml.XmlWriter

开发者 https://www.devze.com 2023-01-10 07:27 出处:网络
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'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 out开发者_如何学JAVAput 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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消