Peter M
Peter M

Reputation: 7493

Setting quote char with XmlWriterSettings or XmlWriter

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.

Upvotes: 2

Views: 1309

Answers (2)

Peter M
Peter M

Reputation: 7493

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.

Upvotes: 1

jdweng
jdweng

Reputation: 34421

The msdn website is sometimes confusing. Use following :

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

Upvotes: -1

Related Questions