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

serialization: get rid of unwanted xmlns attributes

603 views
Skip to first unread message

matro

unread,
Jul 25, 2002, 5:42:11 AM7/25/02
to
hey,

when my class gets serialized, I'd like not to have the following
attributes in root element:

xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

ho can I get rid of them? I already tried the following:

[XmlRootAttribute(ElementName="myRoot", Namespace="")]

please help!

--------

Francesco "Matro" Martire

RealPopup, the freeware winpopup replacer
http://www.realpopup.it

MaTreo, freeware frontscreen for your Treo
http://www.realpopup.it/matreo

Christoph Schittko

unread,
Jul 25, 2002, 10:31:33 AM7/25/02
to
As you found out, these declarations are not related to a type. The
XmlSerializer creates those to avoid local namespace declaration like for
the xsi:type attribut, for example.

To get rid of them you have to pass in an empty new XmlSerializerNamespaces
object to the Serialize() call:

serializer.Serialize( writer, yourObject, new XmlSerializerNamespaces() );

HTH,
--
Christoph Schittko
Software Architect
Mshow - a division of InterCall

"matro" <ma...@usa.net> wrote in message
news:mmhvjug3vphsj1p9o...@4ax.com...

matro

unread,
Jul 25, 2002, 11:36:41 AM7/25/02
to
On Thu, 25 Jul 2002 09:31:33 -0500, "Christoph Schittko"
<ChristophDo...@austin.rr.com> wrote:

>To get rid of them you have to pass in an empty new XmlSerializerNamespaces
>object to the Serialize() call:
>
>serializer.Serialize( writer, yourObject, new XmlSerializerNamespaces() );

it doesn't work for me:

// this is to tell XmlSerializer to use UTF8 encoding
System.IO.MemoryStream mystr=new System.IO.MemoryStream();
XmlSerializer myser=new XmlSerializer(typeof(ftService.ftCommand));
XmlTextWriter writer = new System.Xml.XmlTextWriter(mystr,
System.Text.Encoding.UTF8);

// as Christoph said
myser.Serialize(writer, myCommand, new XmlSerializerNamespaces());


on the other hand, the initial serialized class looks like the
following:

[XmlRootAttribute(ElementName="myCommand")]
public class ftCommand


produced XML looks like the following:

<?xml version="1.0" encoding="utf-8"?><myCommand

...

Christoph Schittko

unread,
Jul 25, 2002, 12:00:16 PM7/25/02
to
Yeah, I should not post before I had time for my morning coffee ...

You actually have to add an empty namespace declaration to the
XmlSerializerNamespaces object:

XmlSerializerNamespaces ns = new XmlSerializerNamespaces();
ns.Add( "", "" );
myser.Serialize(writer, myCommand, ns );

will do the trick.

HTH,
--
Christoph Schittko
Software Architect
Mshow - a division of InterCall

"matro" <ma...@usa.net> wrote in message

news:aj60kuge5bdttqln5...@4ax.com...

matro

unread,
Jul 26, 2002, 4:00:09 AM7/26/02
to
On Thu, 25 Jul 2002 11:00:16 -0500, "Christoph Schittko"
<ChristophDo...@austin.rr.com> wrote:

>Yeah, I should not post before I had time for my morning coffee ...

:-)

warning: I still got to take it this morning...

>You actually have to add an empty namespace declaration to the
>XmlSerializerNamespaces object:

thank you Cristoph, as it works.

now, I got another place where I take advantage of serializing,
through the XmlMessageFormatter class, which is intended to use with
MSMQ serialization support. I'd like to have the same (ie. get rid of
those xmlns attributes) with it. the class internally uses
XmlSerializer, but I don't know how to 'reach' it to tell about blank
namespaces.

Christoph Schittko

unread,
Jul 26, 2002, 12:03:43 PM7/26/02
to
OK ... I drank my coffee this morning ;)

The XmlMessageFormatter does not expose access to the serializers it uses,
or you could override the Write method and call the Serialize() method
accordingly.

Looks like you only choice is to build your own message formatter that
implements IMessageFormatter. It's not hard, merely a hashtable of
XmlSerializer objects.

HTH,
--
Christoph Schittko
Software Architect
Mshow - a division of InterCall

"matro" <ma...@usa.net> wrote in message

news:8902kukd2a06hfh12...@4ax.com...

matro

unread,
Jul 27, 2002, 9:23:04 AM7/27/02
to
On Fri, 26 Jul 2002 11:03:43 -0500, "Christoph Schittko"
<ChristophDo...@austin.rr.com> wrote:

>OK ... I drank my coffee this morning ;)

:-)

>Looks like you only choice is to build your own message formatter that
>implements IMessageFormatter. It's not hard, merely a hashtable of
>XmlSerializer objects.

this could be interesting to do, I'll think about it. thank you for
support you're giving to all ng readers!

0 new messages