How can I generate XML with CR, instead of CRLF in XmlTextWriter
Asked Answered
J

4

5

I'm generating XML via XmlTextWriter.

The file looks good to my eyes, validates (at wc3), and was accepted by the client.

But a client vendor is complaining that the line-endings are CRLF, instead of just CR.

Well, I'm on a Win32 machine using C#, and CRLF is the Win32 standard line-ending. Is there any way to change the line-endings in the XmlTextWriter?

Also -- shouldn't the line-endings not matter to a proper XML parser?

see also: What are carriage return, linefeed, and form feed?

NOTE: looks like the only answer is a sideways solution -- you have to use the XmlWriter instead of the XmlTextWriter

Joesphjoete answered 5/8, 2010 at 13:48 Comment(2)
Note that the popular other way is to use a single LF, not a CR.Afrikaner
new XmlTextWriter() has been deprecated since .NET 2.0. Use XmlWriter.Create instead.Claudicant
J
6

of course, moments after asking, I find a clue on MSDN (that I couldn't find via google) that refers to XmlWriterSettings.NewLineChars

which then led me to the unaccepted answer on SO: Writing XMLDocument to file with specific newline character (c#)

It's all in the terminology.....

Joesphjoete answered 5/8, 2010 at 13:56 Comment(1)
I accepted my own answer after a week had passed with nothing upvoted higher; I was waiting to see if something new would appear.Joesphjoete
C
5

Use the XmlWriterSettings to set what you want as your end of line char.

XmlWriterSettings mySettings = new XmlWriterSettings();
mySettings.NewLineChars = "\r";

XmlWriter writer = XmlWriter.Create(
                         new StreamWriter(@"c:\temp\hello.xml", mySettings);

I don't know where end of line characters would matter. I haven't run into it before.

Calpe answered 5/8, 2010 at 13:55 Comment(0)
E
1

What line ending is used should not matter to a properly implemented parser (see the spec), I quote (emphasis mine):

To simplify the tasks of applications, the XML processor must behave as if it normalized all line breaks in external parsed entities (including the document entity) on input, before parsing, by translating both the two-character sequence #xD #xA and any #xD that is not followed by #xA to a single #xA character.

Therefore, you should be fine with the way you have it right now. You might want to ask what the client vendor is actually doing there, chances are that they are Doing it Wrong.

Elastance answered 5/8, 2010 at 13:56 Comment(1)
I'm with you on that one, but this was the vendor of a client, so we had no pull or push to say they had a problematic implementation.Joesphjoete
W
1

Use the XmlWriterSettings.NewLineChars property.

Woodruff answered 5/8, 2010 at 13:56 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.