XDocument read and save an XML file vs. indent and new line handling

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
I have a XML document with text nodes which contain new line characters (e.g. rn or only n or r). These characters should be preserved as they are when reading and writing the file.
I achieve this by reading the file like this ...

<div style="color:Black; background-color:White
<pre>XDocument doc = XDocument.Load(<span style="color:Blue new
XmlTextReader(<span style="color:Blue new
StringReader(File.ReadAllText(source))));
[/code]

... and saving it like this:

<div style="color:Black; background-color:White
<pre>XmlWriterSettings xws = <span style="color:Blue new
XmlWriterSettings();
xws.NewLineHandling = NewLineHandling.None;
xws.Indent = <span style="color:Blue true
;

<span style="color:Blue using
(XmlWriter xw = XmlWriter.Create(filePath, xws))
{
<span style="color:Blue this
.Document.Save(xw);
}
[/code]


My problem is that I want to force a pretty XML formatting of the file. So that lines like

<div style="color:Black; background-color:White
<pre><span style="color:Blue <?xml version="1.0" encoding="utf-8"?><br/>
<Users><br/>
<User><Name>user1</Name></User><User><Name>user2</Name></User><User><Name>user3</Name></User><br/>
</Users>

[/code]


get a nice look.

So how can I achieve this?
Either I get a nice looking file or the line breaks in the text content get normalized.

regards,
Robert

View the full article
 
Back
Top