Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

XmlSerializer does not encode special characters like &

2,378 views
Skip to first unread message

Sebastian Daser

unread,
Oct 15, 2009, 11:31:32 AM10/15/09
to
I have a class named Config that I would like to serialize to XML using
XmlSerializer. That class has a couple of string properties that may contain
special characters like <, >, &, etc. I am currently using the following
method to serialize the instance (m_Config) of the class:

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

Christian Treffler

unread,
Oct 15, 2009, 1:13:46 PM10/15/09
to
Sebastian Daser schrieb:

> 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

Martin Honnen

unread,
Oct 15, 2009, 1:37:58 PM10/15/09
to
Sebastian Daser wrote:
> I have a class named Config that I would like to serialize to XML using
> XmlSerializer. That class has a couple of string properties that may contain
> special characters like <, >, &, etc. I am currently using the following
> method to serialize the instance (m_Config) of the class:
>
> 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.

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/

Sebastian Daser

unread,
Oct 19, 2009, 2:30:06 PM10/19/09
to
I have found out now.... I need to use XmlTextWriter.

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

0 new messages