Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / March 2007

Tip: Looking for answers? Try searching our database.

XmlSerializer without namespaces?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Jens Weiermann - 12 Feb 2007 07:32 GMT
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
Kevin Spencer - 12 Feb 2007 12:03 GMT
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.

Signature

HTH,

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

The shortest distance between 2 points is a curve.

> Hi,
>
[quoted text clipped - 10 lines]
> Thanks!
> Jens
Jens Weiermann - 12 Feb 2007 13:37 GMT
> 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
Kevin Spencer - 12 Feb 2007 20:29 GMT
You can write your own custom serializer if you really want to. Then you
have full control over the process.

Signature

HTH,

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

The shortest distance between 2 points is a curve.

>> Why is that a problem?
>
[quoted text clipped - 13 lines]
> Thanks!
> Jens
Jens Weiermann - 14 Feb 2007 14:42 GMT
> 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
Jani Järvinen [MVP] - 13 Feb 2007 17:02 GMT
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!

Signature

Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
janij@removethis.dystopia.fi
http://www.saunalahti.fi/janij/

Jens Weiermann - 14 Feb 2007 14:52 GMT
> // create an object to serialize
> StringBuilder testObject = new StringBuilder();
[quoted text clipped - 11 lines]
> // 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="sensaction@mycompany.com"
to="me@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
Jani Järvinen [MVP] - 15 Feb 2007 18:16 GMT
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.

Signature

Regards,

Mr. Jani Järvinen
C# MVP
Helsinki, Finland
janij@removethis.dystopia.fi
http://www.saunalahti.fi/janij/

Zenonas - 16 Mar 2007 11:28 GMT
Try this:

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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.