2
votes

I currently have some code that directly instantiates an XmlTextWriter object, which allows me to set the quote char to a single quote (which I need in order to generate XML to match a legacy system). EG

var fred =  new XmlTextWriter(stream, encoding);
fred.QuoteChar = '\'';

However in Microsoft's documentation for XmlTextWriter class they state:

Starting with the .NET Framework 2.0, we recommend that you create XmlWriter instances by using the XmlWriter.Create method and the XmlWriterSettings class to take advantage of new functionality.

The XmlWriterSettings Class allows you to set various attributes, but the quote character is not one of them, and the XmlWriter class does not expose the quote character like the XmlTextWriter does.

I want to move to XmlWriter as it also solves an indentation problem I have that is not easily solved with the XmlTextWriter, but I still need to set the quote character.

How can I set the quote character in a XmlWriter class?

Technically this is a duplicate of the listed question Can one force XMLWriter to write elements in single quotes?. But If you follow the links in the only answer there you get to a solution for setting the quote character on a XmlTextWriter class. The exact opposite of what I am asking to do.

2
In short: you don't. The ability to use a different quote character is unique to XmlTextWriter. None of the internal implementation classes used by XmlWriter has an option for that (not even an internal or private switch). Disclaimer: I only browsed the disassembly for System.Xml on my machine (.NET 4.7), other versions might conceivably behave differently. - Jeroen Mostert
@JeroenMostert Well that is frustrating. Now I have to figure out how to get ending tags of empty elements on the same line using XmlTextWriter - Peter M
It's a bit of work, but if you have extremely specific needs for the output (like, remaining compatible with a legacy system that does not accept all valid forms of XML) you may want to consider implementing XmlWriter yourself. You can "mostly" wrap around XmlTextWriter when it does what you want, and do something else when it doesn't. Relying on the Framework classes to do these highly specific things for you (and continuing to do them in new versions) seems unwise. (XmlTextWriter isn't sealed, so deriving from it is also an option, but I'm not sure how far that gets you.) - Jeroen Mostert
@JeroenMostert My legacy system is fun. It fails to recognize the end of tag1 in <tag1 /><tag2>Data</tag2> and thinks that tag1 contains "Data" - Peter M

2 Answers

1
votes

As per the comments from Jeroen, you cannot explicitly set the quote character in a XmlWriter class like you can in an XmlTextWriter class.

Fortunately I figured out how to convince XmlTextWriter to create my desired XML format.

-1
votes

The msdn website is sometimes confusing. Use following :

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlTextWriter writer = (XmlTextWriter)XmlWriter.Create("", settings);
            writer.QuoteChar = '\'';