The following code sample should produce a valid xml file to the
console. However, when I try this in C# (Visual Studio 2003, 1.1
Framework), there is an extra questionmark preceding the rest of the
content.
MemoryStream ms = new MemoryStream();
XmlTextWriter xtw = new XmlTextWriter(m s, Encoding.UTF8);
xtw.Namespaces = false;
xtw.Indentation = 5;
xtw.Formatting = Formatting.Inde nted;
xtw.WriteStartD ocument();
xtw.WriteStartE lement("root");
xtw.WriteStartE lement("People" );
xtw.WriteStartE lement("Person" );
xtw.WriteAttrib uteString("Firs tName", "John");
xtw.WriteAttrib uteString("Last Name", "Smith");
xtw.WriteEndEle ment();
xtw.WriteStartE lement("Person" );
xtw.WriteAttrib uteString("Firs tName", "Jane");
xtw.WriteAttrib uteString("Last Name", "Smith");
xtw.WriteEndEle ment();
xtw.WriteEndEle ment();
xtw.WriteEndEle ment();
xtw.WriteEndDoc ument();
xtw.Flush();
xtw.Close();
Console.WriteLi ne( Encoding.UTF8.G etString( ms.ToArray()));
Everything works as expected when I use ASCII encoding. Any encoding
works when I write to a file instead of a memory stream.
Anyone have the answer to this problem?
Thanks in advance.
console. However, when I try this in C# (Visual Studio 2003, 1.1
Framework), there is an extra questionmark preceding the rest of the
content.
MemoryStream ms = new MemoryStream();
XmlTextWriter xtw = new XmlTextWriter(m s, Encoding.UTF8);
xtw.Namespaces = false;
xtw.Indentation = 5;
xtw.Formatting = Formatting.Inde nted;
xtw.WriteStartD ocument();
xtw.WriteStartE lement("root");
xtw.WriteStartE lement("People" );
xtw.WriteStartE lement("Person" );
xtw.WriteAttrib uteString("Firs tName", "John");
xtw.WriteAttrib uteString("Last Name", "Smith");
xtw.WriteEndEle ment();
xtw.WriteStartE lement("Person" );
xtw.WriteAttrib uteString("Firs tName", "Jane");
xtw.WriteAttrib uteString("Last Name", "Smith");
xtw.WriteEndEle ment();
xtw.WriteEndEle ment();
xtw.WriteEndEle ment();
xtw.WriteEndDoc ument();
xtw.Flush();
xtw.Close();
Console.WriteLi ne( Encoding.UTF8.G etString( ms.ToArray()));
Everything works as expected when I use ASCII encoding. Any encoding
works when I write to a file instead of a memory stream.
Anyone have the answer to this problem?
Thanks in advance.
Comment