I found the following solution: deserialize the text reader into a
XmlDocument object, and then deserialize the it into the MemoryStream.
"SammyBar" <sa******@gmail .comescribió en el mensaje
news:Og******** ******@TK2MSFTN GP02.phx.gbl...
Hi all,
I'm trying to convert the xml obtained from a XmlReader object into a
UTF-8 array. My general idea is to read the XmlReader and write into a
MemoryStream. Then convert the MemoryStream bytes into utf-8.
MemoryStream ms = new MemoryStream();
XmlTextWriter xmlWriter = new XmlTextWriter(m s, new UTF8Encoding(fa lse));
writer.Formatti ng = Formatting.Inde nted;
writer.Namespac es = false;
writer.Indentat ion = 4;
while(xmlReader .Read())
{
xmlWriter.Write (?);
}
xmlWriter.Flush ();
xmlWriter.Close ();
string xml_as_utf8 = Encoding.UTF8.G etString(ms.ToA rray());
This is an error: The memory stream is already utf-8 encoded. Then this
previous line re-encodes from utf-8 to the .NET standard encoding so we loss
the encoding when converting.
The solution I found here looks like a hack: to deceive the .NET by reading
the utf-8 saved stream as default encoding:
string xml_data = Encoding.Defaul t.GetString(ms. ToArray());
The code fragments looks like this: (note my input is SqlXml sqlXml
variable)
// load
XmlReader xmlReader = sqlXml.CreateRe ader();
XmlDocument xDoc = new XmlDocument();
xDoc.Load(xmlRe ader);
xmlReader.Close ();
// save
MemoryStream ms = new MemoryStream();
XmlTextWriter writer = new XmlTextWriter(m s, new UTF8Encoding(fa lse));
writer.Formatti ng = Formatting.Inde nted;
writer.Indentat ion = 4;
xDoc.Save(write r);
writer.Close();
ms.Close();
// convert to utf-8
//string xml_data = Encoding.UTF8.G etString(ms.ToA rray());
string xml_data = Encoding.Defaul t.GetString(ms. ToArray());