When you initialize the HtmlTextWriter, you can set the encoding there - that will setup the XML properly, then you just iterate your result set and write the nodes.
The following example is just static, but you could easily convert it to iterate your dataset and write dynamically. It's in C# so you may need to convert it but there are plenty of free converters online.
Also, you will probably need to convert it to write to the buffer rather than a file.
aspnet Code:
using System;
using System.IO;
using System.Xml;
public class Sample
{
public static void Main()
{
XmlTextWriter writer = new XmlTextWriter("myMedia.xml", System.Text.Encoding.GetEncoding("utf-8"));
//Use automatic indentation for readability.
writer.Formatting = Formatting.Indented;
//Write the root element
writer.WriteStartElement("items");
//Start an element
writer.WriteStartElement("item");
//Add an attribute to the previously created element
writer.WriteAttributeString("rating", "R");
//add sub-elements
writer.WriteElementString("title", "The Matrix");
writer.WriteElementString("format", "DVD");
//End the item element
writer.WriteEndElement(); // end item
//Write some white space between nodes
writer.WriteWhitespace("\n");
//Write a second element using raw string data
writer.WriteRaw("<item>" +
"<title>BloodWake</title>" +
"<format>XBox</format>" +
"</item>");
//Write a third element with formatting in the string
writer.WriteRaw("\n <item>\n" +
" <title>Unreal Tournament 2003</title>\n" +
" <format>CD</format>\n" +
" </item>\n");
// end the root element
writer.WriteFullEndElement();
//Write the XML to file and close the writer
writer.Close();
}
}