472,133 Members | 1,196 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,133 software developers and data experts.

XmlSerializer Namespace problem

Hello,

I have a set of classes I created from schema files using the xsd.exe
tool. I'm using namespaces in the clases ( I had to because I have
some classes with the same name but not the same class )
Here is how a given class wil looks like.

<System.Xml.Serialization.XmlRootAttribute([Namespace]:="mynamespace",
IsNullable:=False)> _
Public Class ClassName

End Class

So far so good.

My problem is that I want to deserialize the xml document back, but
the xml document don't have the namespaces. I use the
XmlAttributeOverrides collection to solve the problem by ignoring the
namespaces, but so far I have to add an XmlAttribute for each type
(Class) I want to override.
something like this:

Dim atts As New XmlAttributes
Dim xover As New XmlAttributeOverrides

atts.Xmlns = False

xover.Add(GetType(ClassName), atts)

MyObject = CType(oSerializer.Deserialize(oReader), ClassName)

Is there a way I can tell the serializer to ignore the namespaces for
all types without having to write each type?

Thanks,
Alexis
Nov 12 '05 #1
5 9886

I'm not sure I understand your situation correctly. It sounds like you
don't need XML namespaces because the (possibly implied) schema your
using does not define a formal namespace.

If you don't need XML namespaces at all, would separate .NET namespaces
for the different classes do the trick? Then don't add XML namespaces at
all?

Otherwise, if you actually need to create XML documents that include the
XML namespace declaration, but deserialize XML documents without the
namespace declaration then you could add a default namespace declaration
to the document to deserialize, for example using an XSLT
transformation. However, this approach will carry a pretty significant
overhead if you're deserializing large documents.

Also, is the type of the deserialized objects relevant, i.e. do you ever
need to serialize an object from a previously deserialized instance or
could you get away with two distinct sets of classes; one that carries
the Xml serialization attributes defining the XML namespaces and one
that doesn't?

HTH,
Christoph Schittko
MS MVP XML
http://weblogs.asp.net/cschittko
-----Original Message-----
From: Alexis [mailto:is******@yahoo.com]
Posted At: Wednesday, November 10, 2004 4:30 PM
Posted To: microsoft.public.dotnet.xml
Conversation: XmlSerializer Namespace problem
Subject: XmlSerializer Namespace problem

Hello,

I have a set of classes I created from schema files using the xsd.exe
tool. I'm using namespaces in the clases ( I had to because I have
some classes with the same name but not the same class )
Here is how a given class wil looks like.

<System.Xml.Serialization.XmlRootAttribute([Namespace]:="mynamespace",
IsNullable:=False)> _
Public Class ClassName

End Class

So far so good.

My problem is that I want to deserialize the xml document back, but
the xml document don't have the namespaces. I use the
XmlAttributeOverrides collection to solve the problem by ignoring the
namespaces, but so far I have to add an XmlAttribute for each type
(Class) I want to override.
something like this:

Dim atts As New XmlAttributes
Dim xover As New XmlAttributeOverrides

atts.Xmlns = False

xover.Add(GetType(ClassName), atts)

MyObject = CType(oSerializer.Deserialize(oReader), ClassName)

Is there a way I can tell the serializer to ignore the namespaces for
all types without having to write each type?

Thanks,
Alexis


Nov 12 '05 #2
Q: If you don't need XML namespaces at all, would separate .NET
namespaces
for the different classes do the trick? Then don't add XML namespaces
at
all?
A: These classes are used by a webservice. When the client add the web
reference the proxy classes gererated brings all the classes together.
So I do need the namespaces but only because of this issue.

Q:Also, is the type of the deserialized objects relevant, i.e. do you
ever
need to serialize an object from a previously deserialized instance or
could you get away with two distinct sets of classes; one that carries
the Xml serialization attributes defining the XML namespaces and one
that doesn't?
A: No I don't need to serialize and deserialize the same object. I
will only deserialize from an xml document that don't have namespaces
and I don't have control over it. but also the class for the object
the deserialize will created has namespaces. (need because first
issue)

I also tried the defaultNamespace, but it didn't work.
MySerializer = New XmlSerializer(Type:=ClassName),
defaultNamespace:="mynamespace")

Ok let me add something else. What I'm doing is a webservices that:

1- Input: takes an object "InputObject"
2- serialize it
3- transform the resulted xml document
4- send the transformed xml doc to a server.
5- receive an xml doc from the server. (no namespaces on it and no
control over it)
6- deserialize it to "OutputObject". (the class has namespaces. also
it is NOT the same class as in the "InputObject")
7- Output: return the "OutputObject" to the client. Again the Input
and Output objects use different set of classes.)

So at point 6 I need to deserialize an xml doc with no namespaces to
an object which class has namespaces on it. I hope I explained myself
better this time.

Ok I can resolve this issue by using XmlAttributeOverrides, but I have
to do it for every class the OutputObject usses. Is there an
"ApplyToAll" kind of feature?

Dim atts As New XmlAttributes
Dim xover As New XmlAttributeOverrides
atts.Xmlns = False
xover.Add(GetType(OutputClassName), atts)
xover.Add(GetType(ClassName1), atts)
xover.Add(GetType(ClassName2), atts)
xover.Add(GetType(ClassName3), atts)
MySerializer = New XmlSerializer(GetType(OutputClassName), xover)
MyObject = CType(oSerializer.Deserialize(oReader), OutputClassName)

Please Help I have being working on this for two days.

Thanks,
Alexis
Nov 12 '05 #3
Hello Christoph Schittko,
You did not answered my question. I guess you are too busy or you just
don't know the answer.
Thanks anyway for trying.
Nov 12 '05 #4

Yes, I do have a paying day job that I have to tend to ... answering
questions in newsgroups is something I do on my personal time. I try my
best to answer timely and follow up, but sometimes other issues (the
ones that help me pay my bills take precedence).

Now with all that ... would you still like me to help or have you
figured it out on your own?

Christoph
-----Original Message-----
From: Alexis [mailto:is******@yahoo.com]
Posted At: Monday, November 15, 2004 10:35 AM
Posted To: microsoft.public.dotnet.xml
Conversation: XmlSerializer Namespace problem
Subject: Re: XmlSerializer Namespace problem

Hello Christoph Schittko,
You did not answered my question. I guess you are too busy or you just
don't know the answer.
Thanks anyway for trying.


Nov 12 '05 #5
No, I have not found a way to resolve it.
I'm sorry for the way I wrote my previous posting. This issue has
frustrated me so much. I'm thinking there may be no way of fixing it.
This XmlSerialization is not completed yet (I beleive). It will not be
the first time microsoft releases something not fully worked out. Any
way I still need to find some work around for it. Well as I said I
already have one but I'm not happy about it. Has that happended to
you? You have finished coding but still beleive there got to be a
better way.
Any way, yes if you still want to take another look to this issue you
are more than welcome. I'll really appreciate it.
Thanks in advance.
/Alexis
Nov 12 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by ajay.sonawane | last post: by
reply views Thread by Alexis | last post: by
3 posts views Thread by Sandy | last post: by
6 posts views Thread by SpOiLeR | last post: by
2 posts views Thread by tomek.romanowski | last post: by
10 posts views Thread by anders | last post: by
reply views Thread by SMart | last post: by
5 posts views Thread by michael.d.pedersen | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.