XmlSerializer without namespaces?

K

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.
 
J

Jens Weiermann

Kevin said:
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
 
K

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.
 
J

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
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
J

Jens Weiermann

Kevin said:
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
 
J

Jens Weiermann

Jani said:
// 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="(e-mail address removed)"
to="(e-mail address removed)"
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
 
J

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
(e-mail address removed)
http://www.saunalahti.fi/janij/
 
G

Guest

Try this:

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top