Home All Groups Group Topic Archive Search About

How to serialize into a string ?

Author
17 May 2006 6:25 AM
Peter Stojkovic
I am using a xlmserializer , to write into a file .
serializer.Serialize(writer, po, ns)

Everything works fine.

But instead of writing into a file, i want serialize into a string.

How can I do this ???





Thanks for any hint

Peter

Author
17 May 2006 8:34 AM
Cerebrus
Hi,

Try this :
------------------------------------------
' I assume po is the object you want to serialize.
Dim sw as New StringWriter()
serializer.Serialize(sw, po)
' Get the serialized string
Console.WriteLine(sw.ToString())
------------------------------------------
HTH,

Regards,

Cerebrus.
Author
17 May 2006 1:10 PM
Peter Stojkovic
Thanks a lot :

The result for serialization is:

<?xml version="1.0" encoding="utf-16"?>
.....

But how can I controt which encoding is used ??
Now always UTF-16 is used.
But I need ti use utf-8

How can i switch to utf-8 instead of using itf-16 ?


Peter





Show quoteHide quote
"Cerebrus" <zorg***@sify.com> wrote in message
news:1147854868.876026.141930@y43g2000cwc.googlegroups.com...
> Hi,
>
> Try this :
> ------------------------------------------
> ' I assume po is the object you want to serialize.
> Dim sw as New StringWriter()
> serializer.Serialize(sw, po)
> ' Get the serialized string
> Console.WriteLine(sw.ToString())
> ------------------------------------------
> HTH,
>
> Regards,
>
> Cerebrus.
>
Author
17 May 2006 2:31 PM
Clive Dixon
Try setting the StringWriter.Encoding property to Encoding.UTF8Encoding?

======================
Clive Dixon
Digita Ltd. (www.digita.com)


Show quoteHide quote
"Peter Stojkovic" <d**@gmx.net> wrote in message
news:uispKMbeGHA.1792@TK2MSFTNGP03.phx.gbl...
> Thanks a lot :
>
> The result for serialization is:
>
> <?xml version="1.0" encoding="utf-16"?>
> ....
>
> But how can I controt which encoding is used ??
> Now always UTF-16 is used.
> But I need ti use utf-8
>
> How can i switch to utf-8 instead of using itf-16 ?
>
>
> Peter
>
>
>
>
>
> "Cerebrus" <zorg***@sify.com> wrote in message
> news:1147854868.876026.141930@y43g2000cwc.googlegroups.com...
>> Hi,
>>
>> Try this :
>> ------------------------------------------
>> ' I assume po is the object you want to serialize.
>> Dim sw as New StringWriter()
>> serializer.Serialize(sw, po)
>> ' Get the serialized string
>> Console.WriteLine(sw.ToString())
>> ------------------------------------------
>> HTH,
>>
>> Regards,
>>
>> Cerebrus.
>>
>
>
Author
17 May 2006 2:38 PM
Peter Stojkovic
The endoding is readonly.


Show quoteHide quote
"Clive Dixon" <clived.noluncheonm***@digita.noluncheonmeat.com> wrote in
message news:ehxsn6beGHA.380@TK2MSFTNGP04.phx.gbl...
> Try setting the StringWriter.Encoding property to Encoding.UTF8Encoding?
>
> ======================
> Clive Dixon
> Digita Ltd. (www.digita.com)
>
>
> "Peter Stojkovic" <d**@gmx.net> wrote in message
> news:uispKMbeGHA.1792@TK2MSFTNGP03.phx.gbl...
>> Thanks a lot :
>>
>> The result for serialization is:
>>
>> <?xml version="1.0" encoding="utf-16"?>
>> ....
>>
>> But how can I controt which encoding is used ??
>> Now always UTF-16 is used.
>> But I need ti use utf-8
>>
>> How can i switch to utf-8 instead of using itf-16 ?
>>
>>
>> Peter
>>
>>
>>
>>
>>
>> "Cerebrus" <zorg***@sify.com> wrote in message
>> news:1147854868.876026.141930@y43g2000cwc.googlegroups.com...
>>> Hi,
>>>
>>> Try this :
>>> ------------------------------------------
>>> ' I assume po is the object you want to serialize.
>>> Dim sw as New StringWriter()
>>> serializer.Serialize(sw, po)
>>> ' Get the serialized string
>>> Console.WriteLine(sw.ToString())
>>> ------------------------------------------
>>> HTH,
>>>
>>> Regards,
>>>
>>> Cerebrus.
>>>
>>
>>
>
>
Author
17 May 2006 2:48 PM
Clive Dixon
Sorry yes, that's what comes from trying to quote from vague memory. I think
the answer is to derive your own class from StringWriter and override the
virtual Encoding property to return UTF8Encoding.

======================
Clive Dixon
Digita Ltd. (www.digita.com)


Show quoteHide quote
"Peter Stojkovic" <d**@gmx.net> wrote in message
news:%23YPYo9beGHA.764@TK2MSFTNGP05.phx.gbl...
> The endoding is readonly.
>
>
> "Clive Dixon" <clived.noluncheonm***@digita.noluncheonmeat.com> wrote in
> message news:ehxsn6beGHA.380@TK2MSFTNGP04.phx.gbl...
>> Try setting the StringWriter.Encoding property to Encoding.UTF8Encoding?
>>
>> ======================
>> Clive Dixon
>> Digita Ltd. (www.digita.com)
>>
>>
>> "Peter Stojkovic" <d**@gmx.net> wrote in message
>> news:uispKMbeGHA.1792@TK2MSFTNGP03.phx.gbl...
>>> Thanks a lot :
>>>
>>> The result for serialization is:
>>>
>>> <?xml version="1.0" encoding="utf-16"?>
>>> ....
>>>
>>> But how can I controt which encoding is used ??
>>> Now always UTF-16 is used.
>>> But I need ti use utf-8
>>>
>>> How can i switch to utf-8 instead of using itf-16 ?
>>>
>>>
>>> Peter
>>>
>>>
>>>
>>>
>>>
>>> "Cerebrus" <zorg***@sify.com> wrote in message
>>> news:1147854868.876026.141930@y43g2000cwc.googlegroups.com...
>>>> Hi,
>>>>
>>>> Try this :
>>>> ------------------------------------------
>>>> ' I assume po is the object you want to serialize.
>>>> Dim sw as New StringWriter()
>>>> serializer.Serialize(sw, po)
>>>> ' Get the serialized string
>>>> Console.WriteLine(sw.ToString())
>>>> ------------------------------------------
>>>> HTH,
>>>>
>>>> Regards,
>>>>
>>>> Cerebrus.
>>>>
>>>
>>>
>>
>>
>
>
Author
17 May 2006 2:36 PM
Clive Dixon
....but remember that the string you get still consists of a sequence of
16-bit characters.

======================
Clive Dixon
Digita Ltd. (www.digita.com)


Show quoteHide quote
"Peter Stojkovic" <d**@gmx.net> wrote in message
news:uispKMbeGHA.1792@TK2MSFTNGP03.phx.gbl...
> Thanks a lot :
>
> The result for serialization is:
>
> <?xml version="1.0" encoding="utf-16"?>
> ....
>
> But how can I controt which encoding is used ??
> Now always UTF-16 is used.
> But I need ti use utf-8
>
> How can i switch to utf-8 instead of using itf-16 ?
>
>
> Peter
>
>
>
>
>
> "Cerebrus" <zorg***@sify.com> wrote in message
> news:1147854868.876026.141930@y43g2000cwc.googlegroups.com...
>> Hi,
>>
>> Try this :
>> ------------------------------------------
>> ' I assume po is the object you want to serialize.
>> Dim sw as New StringWriter()
>> serializer.Serialize(sw, po)
>> ' Get the serialized string
>> Console.WriteLine(sw.ToString())
>> ------------------------------------------
>> HTH,
>>
>> Regards,
>>
>> Cerebrus.
>>
>
>