0

I know there is already a question about this: Why XElement Value property changing \r\n to \n?, but the question here was that XElement converts \r\n into \n.

My situation is a bit different, I store \n in my XML, but if I save the XML to a file or a file stream, I get the \r\n back again (Only in the file). Reading it returns \n.

Attached is small snippet of code to see the result. I can understand that due to https://www.w3schools.com/Xml/xml_syntax.asp, \r\n is converted to \n when writing, but I see no reason why it is written as \r\n to the stream or file.

using System.Xml.Linq;

var text = "Test1" + Environment.NewLine + "Test2" + Environment.NewLine + "Test3";

using MemoryStream stream = new();

var xmlSource = new XElement("Data", text.Replace(Environment.NewLine, "\n"));
xmlSource.Save(stream, SaveOptions.None);
stream.Position = 0;

var xmlTarget = XElement.Load(stream);
Console.WriteLine(xmlTarget.Value);

Is there some explanation on the given behavior?

msedi
  • 1,437
  • 15
  • 25
  • The Save methods add returns automatically and you are adding additional returns. – jdweng Jan 23 '23 at 17:20
  • If you want to change that behavior you may try using a Save overload taking a XmlWriter. For the XmlWriter then you have access to the Settings of that. Especially XmlWriterSettings.NewLineHandling – Ralf Jan 23 '23 at 17:26
  • @Ralf. Thanks, I have read about it. The only problem is that all my interfaces work with XElement and it would be too much effort to change this. – msedi Jan 23 '23 at 17:30
  • @jdweng. I'm not sure what you mean. I need to add the newlines in between and remove the \r before. Where does the Save add additional returns? – msedi Jan 23 '23 at 17:32
  • @msedi Just changing the xmlSource.Save line at multiple spots to something different is to much effort? With a fitting Extension method it would even be a one liner still. – Ralf Jan 23 '23 at 17:33
  • @Ralf. In this situation yes, because I have around 30 methods using it and I currently cannot forsee the negative effects. But I will check what I can do. – msedi Jan 23 '23 at 17:35
  • 1
    _"The only problem is that all my interfaces work with XElement"_ You don't need to drop `XElement` usage, the customized `XmlWriter` can be passed to its `Save` method: https://stackoverflow.com/a/41830950/5114784 – György Kőszeg Jan 23 '23 at 17:45
  • 1
    Microsoft changed the return to the same as Unix recently. They even updated Notepad to work with just the '\r'. The CORE changes in c# were to allow same code to run under both Windows and Linux. So Core enhancement ended up requiring to modify the return character. XML ignores the white space including the returns. – jdweng Jan 23 '23 at 18:15

1 Answers1

0

Well, actually... you can use \r\n or \n in an XML document but a conformant XML parser will normalize either of them to just \n when it reads the document. And usually those line-ending characters are just so that people can read the document more easily, the application which uses the documents normally ignore them. So you've only got a serious problem if the application which uses the document is rejecting or mishandling it.

However, yeah, it's nice for people to be able to read an XML document sometimes.

A. Rokbi
  • 503
  • 2
  • 8
  • Alright. This means that the XElement serialization is that it is normalized with \r\n. That makes sense. – msedi Jan 23 '23 at 17:34
  • 1
    Its "normalized" with what's in Environment.Newline so plattform dependent. – Ralf Jan 23 '23 at 17:40