In .NET, how do I write a UTF-16 XMLDocument to a string with a BOM
I am buildign an [XmlDocument](http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx) on the fly in .NET with an xml document. I then transform that with the [Transform()](http://msdn.microsoft.com/en-us/library/system.xml.xsl.xslcompiledtransform.transform.aspx) method of an [XslCompiledTransform](http://msdn.microsoft.com/en-us/library/system.xml.xsl.xslcompiledtransform.aspx).

The Transform() method threw an exception because an invalid character for the encoding was found in the stream. When I copy/paste the string with the help of the TextVisualizer in Visual Studio into [Altova XmlSpy](http://www.altova.com/xmlspy.html), it does not find an encoding problem.

I tried adding a UTF-16 header to the document to make it render as UTF-16 and calling Transform from the resulting text led to it complain about a BOM. Below is a simplified version of the code I used.

                XmlDocument document = new XmlDocument();
                XmlDeclaration decl = document.CreateXmlDeclaration("1.0", "UTF-16", null);
                document.AppendChild(decl);

                XmlNode root = document.CreateNode(XmlNodeType.Element, "RootNode", "");
                XmlNode nodeOne = document.CreateNode(XmlNodeType.Element, "FirstChild", null);
                XmlNode nodeTwp = doc.CreateNode(XmlNodeType.Element, "Second Child", null);

                root.AppendChild(nodeOne);
                root.AppendChild(nodeTwo);
                document.AppendChild(root);

Which I consequently am writing to a string like so:

            StringBuilder sbXml = new StringBuilder();
            using (XmlWriter wtr = XmlWriter.Create(sbXml))
            {
                xml.WriteTo(wtr);
                // More code that calls sbXml.ToString());
            }

What must I do to add the BOM or get XslCompiledTransform.Transform to not care about the bom?
In .NET, how do I write a UTF-16 XMLDocument to a string with a BOM
===

I am buildign an [XmlDocument](http://msdn.microsoft.com/en-us/library/system.xml.xmldocument.aspx) on the fly in .NET with an xml document. I then transform that with the [Transform()](http://msdn.microsoft.com/en-us/library/system.xml.xsl.xslcompiledtransform.transform.aspx) method of an [XslCompiledTransform](http://msdn.microsoft.com/en-us/library/system.xml.xsl.xslcompiledtransform.aspx).

The Transform() method threw an exception because an invalid character for the encoding was found in the stream. When I copy/paste the string with the help of the TextVisualizer in Visual Studio into [Altova XmlSpy](http://www.altova.com/xmlspy.html), it does not find an encoding problem.

I tried adding a UTF-16 header to the document to make it render as UTF-16 and calling Transform from the resulting text led to it complain about a BOM. Below is a simplified version of the code I used.

                XmlDocument document = new XmlDocument();
                XmlDeclaration decl = document.CreateXmlDeclaration("1.0", "UTF-16", null);
                document.AppendChild(decl);

                XmlNode root = document.CreateNode(XmlNodeType.Element, "RootNode", "");
                XmlNode nodeOne = document.CreateNode(XmlNodeType.Element, "FirstChild", null);
                XmlNode nodeTwp = doc.CreateNode(XmlNodeType.Element, "Second Child", null);

                root.AppendChild(nodeOne);
                root.AppendChild(nodeTwo);
                document.AppendChild(root);

Which I consequently am writing to a string like so:

            StringBuilder sbXml = new StringBuilder();
            using (XmlWriter wtr = XmlWriter.Create(sbXml))
            {
                xml.WriteTo(wtr);
                // More code that calls sbXml.ToString());
            }

What must I do to add the BOM or get XslCompiledTransform.Transform to not care about the bom?