I am building an XmlDocument on the fly in .NET with an xml document. I then transform that with the Transform() method of an XslCompiledTransform.
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, 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?