I worked on a map editor for a tile based game. I want to load and save maps in an XML file.
I learned about the xmlSerializer class and wanted to try it instead of creating my own parser. It wasnβt that hard to figure out. The only problem I am facing right now is that I cannot get rid of the xml declaration. It seems that XmlSettings are not applied at all. Talking with him all night without progress, help is greatly appreciated. Here is the code:
public static void SerializeMap(Array map)
{
XmlWriterSettings xmlSettings = new XmlWriterSettings();
xmlSettings.OmitXmlDeclaration = true;
xmlSettings.ConformanceLevel = ConformanceLevel.Fragment;
xmlSettings.Indent = false;
var xmlSerializer = new XmlSerializer(typeof(Tile));
XmlSerializerNamespaces emptyNameSpace = new XmlSerializerNamespaces();
emptyNameSpace.Add("", "");
StringWriter stringWriter = new StringWriter();
using (XmlWriter xmlWriter = XmlWriter.Create(stringWriter, xmlSettings))
{
for (int z = 0; z < map.GetLength(0); z++)
{
for (int x = 0; x < map.GetLength(1); x++)
{
for (int y = 0; y < map.GetLength(2); y++)
{
xmlSerializer.Serialize(stringWriter, map.GetValue(z, x, y), emptyNameSpace);
}
}
}
}
FileStream fs = new FileStream("Content/tiletest.xml", FileMode.Create);
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(xmlText);
sw.Close();
fs.Close();
}
As I iterate over the objects, each newly written object in the XML file will have an attached xml declaration that will break the xml formatting. Incredibly annoying because everything else works fine with serialization.
source
share