C# write to file >> first byte-order problem

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
I was wondering if you could help with the below:
I have an XML file called test.xml that I need to do some modify, XML file looks similar to the below
<pre class="prettyprint <?xml version="1.0" encoding="utf-8"?>
<someNode>
<someOtherNode>blah blah<someOtherNode>
</someNode>[/code]
<br/>
=========================


<span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <span style="color:#008000; font-size:small <a> <a> <a> <a> <a> <a> <a> <a> <a> <a> <a> <a> <a> <a> <a> <a> <a> <a> <a> <a> <a> <a> <a> <a> <a> <a>
<pre class="prettyprint private string Beautify_XML(XmlDocument doc)
{
//StringBuilder sb = new StringBuilder();
MemoryStream stream = new MemoryStream();
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = " ";
settings.NewLineChars = "rn";
settings.NewLineHandling = NewLineHandling.Replace;
settings.Encoding = Encoding.UTF8;
using (XmlWriter writer = XmlWriter.Create(stream, settings)) {doc.Save(writer); }
return UTF8Encoding.UTF8.GetString(stream.ToArray());
} //end Beautify(XmlDocument doc)[/code]
<br/>


===========================
After the modification I need to write it back to the test.xml
I got a function as below just to format the return string before I write it back to the XML file (overwrite)
<span style="color:#008000; font-size:small and then I call it as follow


<pre class="prettyprint string myBeautiful_XML_string = Beautify_XML(myXmlDocument);
//Now write it back to the XML
System.IO.File.WriteAllText(my_xml_path, myBeautiful_XML_string);[/code]
The problem is after the XML file is written, there is some weird character (trunk) at start of file, which I googled and someone call it "first byte-order".
What have I done wrong? and how to fix it? Thanks you in advance.


View the full article
 
Back
Top