13

How do remove the BOM from an XML file that is being created?

I have tried using the new UTF8Encoding(false) method, but it doesn't work. Here is the code I have:

XmlDocument xmlDoc = new XmlDocument();
XmlTextWriter xmlWriter = new XmlTextWriter(filename, new UTF8Encoding(false));
xmlWriter.Formatting = Formatting.Indented;
xmlWriter.WriteProcessingInstruction("xml", "version='1.0' encoding='UTF-8'");
xmlWriter.WriteStartElement("items");
xmlWriter.Close();
xmlDoc.Load(filename);
XmlNode root = xmlDoc.DocumentElement;
XmlElement item = xmlDoc.CreateElement("item");
root.AppendChild(item);
XmlElement itemCategory = xmlDoc.CreateElement("category");
XmlText itemCategoryText = xmlDoc.CreateTextNode("test");
item.AppendChild(itemCategory);
itemCategory.AppendChild(itemCategoryText);
xmlDoc.Save(filename);
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Chris
  • 229
  • 2
  • 6
  • 14
  • FYI, you should not use `new XmlTextReader()` or `new XmlTextWriter()`. They have been deprecated since .NET 2.0. Use `XmlReader.Create()` or `XmlWriter.Create()` instead. – John Saunders Jun 25 '14 at 01:17

2 Answers2

31

You're saving the file twice - once with XmlTextWriter and once with xmlDoc.Save. Saving from the XmlTextWriter isn't adding a BOM - saving with xmlDoc.Save is.

Just save to a TextWriter instead, so that you can specify the encoding again:

using (TextWriter writer = new StreamWriter(filename, false,
                                            new UTF8Encoding(false))
{
    xmlDoc.Save(writer);
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • Hi Jon, thanks for the quick reply, so are you saying to remove the XmlTextWriter section at the beginning and only use the TextWriter at the end of the method? – Chris Nov 18 '09 at 17:06
  • Well it's not clear what your code is meant to be doing. Why are you currently saving the file and then reloading it? – Jon Skeet Nov 18 '09 at 18:07
  • Im just trying to create an XML file with a range of different nodes inside it. To be honest i was in a rush and pulled the section from another site and changed it where appropriate. – Chris Nov 19 '09 at 08:11
  • @Chris: Okay, in that case yes, you can move the XmlTextWriter part to the bottom - or just use `XmlDocument.Save` in the way I've shown in the answer. – Jon Skeet Nov 19 '09 at 08:18
  • Hi Jon, thanks for your help, i finally managed to get this working. – Chris Nov 19 '09 at 10:20
  • I'm not sure why but this does not work for me the second parameter of StreamWriter must be a bool. – rekire Nov 11 '13 at 07:56
  • @rekire: Well you haven't told us *anything* about your context, which makes it hard to help you. What sort of app are you writing? – Jon Skeet Nov 11 '13 at 08:55
  • @JonSkeet I'm implementing a console project (targeting .net 4.5). I Could fix this problem by using this constructor: `new StreamWriter(outfile, false, new UTF8Encoding(false))`. It's just wired that a constructor seems to been removed. – rekire Nov 11 '13 at 09:06
  • @rekire: I think it was my mistake, actually - it looks like there's a *stream* version with just an encoding, but not a *string* version. Will edit the answer. – Jon Skeet Nov 11 '13 at 09:21
0

I'd write the XML to a string(builder) instead and then write that string to file.

Dan Byström
  • 9,067
  • 5
  • 38
  • 68
  • This approach is slower than necessary and will eat up extra memory if your XML data set is large enough, as you're adding an unnecessary middle step.. Best to write directly to the file using Xml[Text]Writer as indicated by the other answers. – user169771 Mar 31 '16 at 15:09