XML String Serialization With Chars < 32

K

Kieran Benton

Hi everyone,
Sorry but I'm asking this for a friend! For some reason he needs to store a
string with characters of ASCII value <32 (in this case 18 and 10) as a
string in a serialized XML file. Apparently its something to do with the
Anarchy Online online gaming community and they expect to see these
characters, which debunks my first thought of just adding 32 to each
character before writing it out! Xml is needed as it has to be human
readable. If you try the following example, the Xml parser throws an invalid
xmldocument exception on read in of the xml file (the file is outputted
correctly with html style references to the <32 characters). Is this a
problem with the framework or are we both missing something?

Many thanks, Kieran

using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

public class Test
{
[STAThread]
static void Main(string[] args)
{
Test test = new Test();
}

public class Data
{
public string text = "Some random " + (char)10 + (char)18 + " random
stuff";
}

public Data data = new Data();

public Test()
{
Save();
Load();
}

public void Save()
{
TextWriter writer = null;

XmlSerializer serializer = new XmlSerializer(typeof(Test.Data));
writer = new StreamWriter(@"test.xml");
serializer.Serialize(writer,data);
writer.Close();
}

public void Load()
{
TextReader reader = null;

XmlSerializer serializer = new XmlSerializer(typeof(Test.Data));
reader = new StreamReader(@"Test.xml");
data = (Test.Data)serializer.Deserialize(reader);
reader.Close();
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top