Home All Groups Group Topic Archive Search About

XmlSerializer without namespaces?

Author
12 Feb 2007 7:32 AM
Jens Weiermann
Hi,

I'm using XmlSerializer objects to - surprise - serialize objects to xml.
My problem is that it includes the xml namespace references

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

for every single object which 1) isn't necessary in this case and 2) makes
the output xml more difficult to read. My question is: Is there a way to
avoid that?

Thanks!
Jens

Author
12 Feb 2007 12:03 PM
Kevin Spencer
Why is that a problem? There is no way to avoid it if you are serializing an
instance of a class to XML, because to remove the namespace references would
make it invalid as a serialized instance of a class (it could not be
de-serialized). If you really want to remove the namespaces, you'll have to
use your own code to do it.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

The shortest distance between 2 points is a curve.

Show quote
"Jens Weiermann" <spamgoeshere@wexman.com> wrote in message
news:1h7vmgt16alhb.dlg@prog02.wexman.com...
> Hi,
>
> I'm using XmlSerializer objects to - surprise - serialize objects to xml.
> My problem is that it includes the xml namespace references
>
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>
> for every single object which 1) isn't necessary in this case and 2) makes
> the output xml more difficult to read. My question is: Is there a way to
> avoid that?
>
> Thanks!
> Jens
Author
12 Feb 2007 1:37 PM
Jens Weiermann
Kevin Spencer wrote:

> Why is that a problem?

well, not a "real" problem, but as I originally wrote it makes the file
harder to read. Plus it increases file size significantly.

> There is no way to avoid it if you are serializing an instance of a class
> to XML, because to remove the namespace references would make it invalid
> as a serialized instance of a class (it could not be de-serialized).

Sorry, but that is not true. I can remove these namespace refences and my
objects de-serialize just fine...

Anyway, if I have to leave them in there, is it possible to have them
appear once in the xml document instead of once for every single object?

Thanks!
Jens
Author
12 Feb 2007 8:29 PM
Kevin Spencer
You can write your own custom serializer if you really want to. Then you
have full control over the process.

--
HTH,

Kevin Spencer
Microsoft MVP
Software Composer
http://unclechutney.blogspot.com

The shortest distance between 2 points is a curve.

Show quote
"Jens Weiermann" <spamgoeshere@wexman.com> wrote in message
news:1auy7do3c3hx8$.dlg@prog02.wexman.com...
> Kevin Spencer wrote:
>
>> Why is that a problem?
>
> well, not a "real" problem, but as I originally wrote it makes the file
> harder to read. Plus it increases file size significantly.
>
>> There is no way to avoid it if you are serializing an instance of a class
>> to XML, because to remove the namespace references would make it invalid
>> as a serialized instance of a class (it could not be de-serialized).
>
> Sorry, but that is not true. I can remove these namespace refences and my
> objects de-serialize just fine...
>
> Anyway, if I have to leave them in there, is it possible to have them
> appear once in the xml document instead of once for every single object?
>
> Thanks!
> Jens
Author
14 Feb 2007 2:42 PM
Jens Weiermann
Kevin Spencer wrote:

> You can write your own custom serializer if you really want to. Then you
> have full control over the process.

Of course I can - but that's what I wanted to avoid.

The question is: As the XmlSerializer doesn't need these namespace
references for de-serializing, then why does it put them there when
serializing? This seems rather odd to me, so I thought there would be a
simple way to turn it off.

Jens
Author
13 Feb 2007 5:02 PM
Jani Järvinen [MVP]
Hello Jens,

> My problem is that it includes the xml namespace references
>
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xmlns:xsd="http://www.w3.org/2001/XMLSchema"
>
> for every single object which 1) isn't necessary in this case.

The references themselves shouldn't do no harm, but if you want to strip
them out, it only requires few lines of extra code.

I haven't investigated very deeply into the xml serialization framework
and/or the possible customizations and overrides the .NET class library
supports, so it *might* indeed be possible to do this the "proper" way. In
the meantime I would suggest that you use a simple XmlDocument object to
strip away the unnecessary namespace references.

Here's a simple example:

-----------------
// create an object to serialize
StringBuilder testObject = new StringBuilder();
// create xml serializer
XmlSerializer xmlSerializer = new XmlSerializer(testObject.GetType());
// serialize to memory stream & load xml
MemoryStream stream = new MemoryStream();
XmlDocument doc = new XmlDocument();
xmlSerializer.Serialize(stream, testObject);
stream.Position = 0;
doc.Load(stream);
stream.Close();
// strip out default namespaces "xmlns:xsi" and "xmlns:xsd"
doc.DocumentElement.Attributes.RemoveAll();
// save to file
doc.Save(@"C:\Temp\Serialization Test.xml");
-----------------

Would this be a solution for you?

Thanks!

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
ja***@removethis.dystopia.fi
http://www.saunalahti.fi/janij/
Author
14 Feb 2007 2:52 PM
Jens Weiermann
Jani Järvinen [MVP] wrote:

Show quote
> // create an object to serialize
> StringBuilder testObject = new StringBuilder();
> // create xml serializer
> XmlSerializer xmlSerializer = new XmlSerializer(testObject.GetType());
> // serialize to memory stream & load xml
> MemoryStream stream = new MemoryStream();
> XmlDocument doc = new XmlDocument();
> xmlSerializer.Serialize(stream, testObject);
> stream.Position = 0;
> doc.Load(stream);
> stream.Close();
> // strip out default namespaces "xmlns:xsi" and "xmlns:xsd"
> doc.DocumentElement.Attributes.RemoveAll();
> // save to file
> doc.Save(@"C:\Temp\Serialization Test.xml");

Thanks for your answer.

As you're using DocumentElement.Attributes(), I guess that's were you're
assuming the namespace references to be put - if it were that way, I could
very well live with that. But instead, the references appear on every
object that's serialized - here's an example:

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<sensaction>
  <trigger name="Alle 10 Sekunden" type="TimerTrigger">
    <TimerTriggerSettings
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
interval="10000" />
  </trigger>
  <sensor name="Madmortem Realm Status" type="WowRealmStatusSensor">
    <WowRealmStatusSensorSettings
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
RealmName="Madmortem" />
  </sensor>
  <actor name="Mail an JW" type="SmtpActor">
    <SmtpActorSettings
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
host="wilmsmail"
from="sensact***@mycompany.com"
to=***@mycompany.com"
subject="Test"
bodyFormatter="Mail" />
  </actor>
</senaction>

Here's what I'm doing to create the above:

I'm using a XmlTextWriter and manually create the <sensaction> and
<trigger> elements using XmlTextWriter.WriteStartElement() and
WriteEndElement(). Then I'm creating the XmlSerializer and call
Serialize(XmlTextWriter, object) to serialize my objects...

Any ideas?

Jens
Author
15 Feb 2007 6:16 PM
Jani Järvinen [MVP]
Hello!

> As you're using DocumentElement.Attributes(), I guess that's were you're
> assuming the namespace references to be put - if it were that way, I could
> very well live with that.

No, my intention was only to write a simple example to get you started. Of
course, if you serialize multiple objects, you would need to remove the
references in multiple places. Or, you use XPath.

Other than writing your own custom serializer, I'm not aware of a way with
which you could disable the automatic namespace creation. You must judge
whether it is work the extra work to get rid of them, or is it just
cosmetic.

--
Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
ja***@removethis.dystopia.fi
http://www.saunalahti.fi/janij/
Author
16 Mar 2007 10:28 AM
Zenonas
Try this:

XmlSerializer serializer = new XmlSerializer(typeof(Order));
XmlSerializerNamespaces xmlnsEmpty = new XmlSerializerNamespaces();
xmlnsEmpty.Add("", "");
XmlTextWriter writer = new XmlTextWriter(fileName);
serializer.Serialize(writer, order, xmlnsEmpty);

AddThis Social Bookmark Button