Private Sub SerializeConfiguration()
Dim userStorage As IsolatedStorageFile =
IsolatedStorageFile.GetUserStoreForAssembly
Dim fileStream As IsolatedStorageFileStream = New
IsolatedStorageFileStream("Config.XML", IO.FileMode.Create, userStorage)
Dim serializer As XmlSerializer = New XmlSerializer(GetType(Config))
serializer.Serialize(fileStream, m_Config)
fileStream.Close()
End Sub
The problem with this method is that special characters (e.g. <, >, &) are
not encode (e.g. to <, >, &), so the output is not well-formed XML.
Is there an easy and proper way to make XmlSerializer encode special
characters. I do not want to encode the data inside the instance of my Config
class.
Thanks and kind regards
Sebastian
> The problem with this method is that special characters (e.g. <, >, &) are
> not encode (e.g. to <, >, &), so the output is not well-formed XML.
'<' and '>' are enclosing characters of the tags. Having these in the
content might mix up tag hierarchy.
Quote from http://en.wikipedia.org/wiki/Xml#Characters_and_escaping
| The characters "<" and "&" are key syntax markers and may never appear in content
> Is there an easy and proper way to make XmlSerializer encode special
> characters.
I guess not. I have seen the same behaviour.
CU,
Christian
Are you posting using the web interface to the newsgroups?
Unfortunately discussing encoding questions that way is difficult as any
entity references you might have used are not showing up.
> Is there an easy and proper way to make XmlSerializer encode special
> characters. I do not want to encode the data inside the instance of my Config
> class.
I don't see that problem.
With e.g.
class Program
{
static void Main(string[] args)
{
Foo foo1 = new Foo() { Bar = "a < b && b < c" };
XmlSerializer ser = new XmlSerializer(typeof(Foo));
ser.Serialize(Console.Out, foo1);
Console.WriteLine();
}
}
public class Foo
{
public string Bar { get; set; }
}
the characters '<' and '&' are properly escaped
(as '& l t;' and '& a m p;', only without the spaces I inserted in the
hope the web interface shows what I want).
--
Martin Honnen --- MVP XML
http://msmvps.com/blogs/martin_honnen/
My code now looks like:
Private Sub SerializeConfiguration()
Dim userStorage As IsolatedStorageFile =
IsolatedStorageFile.GetUserStoreForAssembly
Dim fileStream As IsolatedStorageFileStream = New
IsolatedStorageFileStream("Config.XML", IO.FileMode.Create, userStorage)
Dim xmlTextWriter as new XmlTextWriter(fileStream, Text.Encoding.Unicode)
Dim serializer As XmlSerializer = New XmlSerializer(GetType(Config))
serializer.Serialize(xmlTextWriter, m_Config)
fileStream.Close()
End Sub