5

Here's my code:

var ptFirstName = tboxFirstName.Text;
writer.WriteAttributeString("first",ptFirstName);

note that ptFirstName will end up in double quotes even if I use

ptFirstName = ptFirstName.Replace("\"","'"); 

This does not work either since the writer will still force double quotes in my file as follows:

when in fact I need (don't ask me why - it's a long story...)

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
user2058253
  • 191
  • 1
  • 2
  • 4

1 Answers1

6

Yes, you can set what character is used for quotes by setting the XmlTextWriter.QuoteChar property. See http://msdn.microsoft.com/en-ca/library/system.xml.xmltextwriter.quotechar.aspx for details.

But this means you have to create an XmlTextWriter object for this to work.

Peter Ritchie
  • 35,463
  • 9
  • 80
  • 98
  • OK, that worked nicely! using (XmlTextWriter writer = new XmlTextWriter(targetFile, System.Text.Encoding.UTF8)) { writer.QuoteChar = '\''; writer.Formatting = Formatting.Indented; – user2058253 Mar 17 '13 at 03:20
  • 1
    It's not correct because it only works on the to be deprecated `XmlTextWriter` extension and not the preferred `XmlWriter` – Yoshiya Nov 06 '18 at 21:21
  • @yoshiya can you reference where it is detailed that using an `XmlWriter` property only works with `XmlTextWriter`? – Peter Ritchie Nov 13 '18 at 14:25
  • Okay, I see what you're saying now @Yoshiya XmlTextWriter is the only class that has a QuoteChar property and XmlWriter.Create won't create an XmlTextWriter object; so you have to use XmlTextWriter explicitly. – Peter Ritchie Dec 26 '18 at 14:09
  • @james You can use `XmlTextWriter` with `XmlDocument`. This `XmlDocument` read/write overview touches on `QuoteChar`: https://learn.microsoft.com/en-us/dotnet/standard/data/xml/saving-and-writing-a-document and has an example of using `XmlTextWriter` and `XmlDocument` – Peter Ritchie Mar 02 '20 at 13:34