2

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.

Peter M
  • 7,309
  • 3
  • 50
  • 91
  • Possible duplicate of [Can one force XMLWriter to write elements in single quotes?](https://stackoverflow.com/questions/15457011/can-one-force-xmlwriter-to-write-elements-in-single-quotes) – Sinatr Nov 10 '17 at 15:01
  • 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 Nov 10 '17 at 15:04
  • @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 Nov 10 '17 at 15:18
  • 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 Nov 10 '17 at 15:21
  • @JeroenMostert My legacy system is fun. It fails to recognize the end of `tag1` in `Data` and thinks that `tag1` contains "Data" – Peter M Nov 10 '17 at 15:25
  • It may well not be using an actual XML parser at all, but some homebrew monstrosity that uses regexes to recognize fragments (or other manual code, or maybe an HTML parser). In which case it will probably fail on the finer points like character entities as well. Depending on how bad this gets and how far your document indents, you could also consider not using an `XmlWriter` at all and instead cobble together the strings in exactly the right way. Obviously, there's a line somewhere where this is more or less appropriate; there's more chance of generating invalid XML if you do things manually. – Jeroen Mostert Nov 10 '17 at 15:29
  • @JeroenMostert Well the legacy XML is actually generated by a 3rd party commercial library that someone actually paid for. But I think that in this case writing my own XML generator may be the best solution. Which is sad as it does deserialize quite nicely with the standard frameworks. – Peter M Nov 10 '17 at 15:37

2 Answers2

1

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.

Peter M
  • 7,309
  • 3
  • 50
  • 91
-1

The msdn website is sometimes confusing. Use following :

            XmlWriterSettings settings = new XmlWriterSettings();
            settings.Indent = true;
            XmlTextWriter writer = (XmlTextWriter)XmlWriter.Create("", settings);
            writer.QuoteChar = '\'';
jdweng
  • 33,250
  • 2
  • 15
  • 20
  • That won't work. `XmlWriter.Create` does not *ever* return an `XmlTextWriter` (on recent versions of the Framework, at least, I didn't check how far back things go). – Jeroen Mostert Nov 10 '17 at 15:03
  • @JeroenMostert Does that apply even to what is returned from `XmlTextWriter.Create()`? – Peter M Nov 10 '17 at 15:12
  • @PeterM: There is no separate `XmlTextWriter.Create` method. It's a static method of `XmlWriter`. You can invoke it through `XmlTextWriter` (since `XmlTextWriter` derives from `XmlWriter`), but it's the same method. – Jeroen Mostert Nov 10 '17 at 15:14