Hi I am trying to save xml to a file and it keeps adding different character at the start of file.
Code I have is
string strCXML2;
strCXML2 = "<input type=" + "\"" + "hidden" + "\"" + " name=" + "\"" + "cxml-urlencoded" + "\"" + " value=" + "\"";
strCXML2 = strCXML2 + "<!DOCTYPE cXML SYSTEM " + "\"" + "http://xml.cxml.org/schemas/cXML/1.2.023/cXML.dtd" + "\"" + ">";
//<snip>
BinaryWriter bw5 = new BinaryWriter(File.Open(Server.MapPath("~/data/test/test19.xml"), FileMode.OpenOrCreate))
bw5.Write(strCXML2);
bw5.Close();
This return following file in xml “}” at start each time it is some other junk character
}<input type="hidden" name="cxml-urlencoded" value="<!DOCTYPE cXML SYSTEM "http://xml.cxml.org/schemas/cXML/1.2.023/cXML.dtd">
Any ideas why it is doing this?
The characters it is writing is the encoded length of the string, because: it is not expecting to write a text file.
BinaryWriterwrites… binary. It is writing the string in an encoded binary way that it can read back withBinaryReader.ReadString.Instead, use just:
The
BinaryWriter.Write(string)method’s documentation explains all this:and, helpfully, links to a separate section about writing text files