|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
XmlSerializer without namespaces?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 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. -- Show quoteHTH, Kevin Spencer Microsoft MVP Software Composer http://unclechutney.blogspot.com The shortest distance between 2 points is a curve. "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 Kevin Spencer wrote:
> Why is that a problem? well, not a "real" problem, but as I originally wrote it makes the fileharder to read. Plus it increases file size significantly. > There is no way to avoid it if you are serializing an instance of a class Sorry, but that is not true. I can remove these namespace refences and my> 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). 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 You can write your own custom serializer if you really want to. Then you
have full control over the process. -- Show quoteHTH, Kevin Spencer Microsoft MVP Software Composer http://unclechutney.blogspot.com The shortest distance between 2 points is a curve. "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 Kevin Spencer wrote:
> You can write your own custom serializer if you really want to. Then you Of course I can - but that's what I wanted to avoid.> have full control over the process. 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 Hello Jens,
> My problem is that it includes the xml namespace references The references themselves shouldn't do no harm, but if you want to strip > > 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. 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/ Jani Järvinen [MVP] wrote:
Show quote > // create an object to serialize Thanks for your answer.> 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"); 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 Hello!
> As you're using DocumentElement.Attributes(), I guess that's were you're No, my intention was only to write a simple example to get you started. Of > assuming the namespace references to be put - if it were that way, I could > very well live with that. 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/ |
|||||||||||||||||||||||