By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,838 Members | 1,623 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,838 IT Pros & Developers. It's quick & easy.

How to tell the XmlSerializer to ignore all namespaces

P: n/a
Hello,

Is there a way of telling the XmlSerializer to ignore all namespaces
when deserializing.

I'm using XmlAttributeOverrides, but I have
to do it for every class the OutputObject usses. Is there an
"ApplyToAll" kind of feature?

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

Ok let me describe the whole process. 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.

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 4 days.

Thanks,
Alexis
Nov 12 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
can I see an example of the OutputObject?

Let's suppose it is really simple, like the following:
<Output>
<element1>value1</element1>
<element2 name="something"/>
<element3>
<subelt>foo</subelt>
</element3>
</Output>

If I run this through xsd.exe, I can infer an XSD from it. Then if I run
xsd.exe /c on that XSD, I can generate a class that the XmlSerializer will
produce on de-serialization of the example XML document. The generated
Output class is annotated with
[System.Xml.Serialization.XmlRootAttribute(Namespac e="", IsNullable=false)]
How are you defining OutputObject and how is the mapping between the output
xml document and the OutputObject class defined?

-Dino

--
Dino Chiesa
Microsoft Developer Division
d i n o c h @ OmitThis . m i c r o s o f t . c o m

"Alexis" <is******@yahoo.com> wrote in message
news:b2**************************@posting.google.c om...
Hello,

Is there a way of telling the XmlSerializer to ignore all namespaces
when deserializing.

I'm using XmlAttributeOverrides, but I have
to do it for every class the OutputObject usses. Is there an
"ApplyToAll" kind of feature?

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

Ok let me describe the whole process. 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.

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 4 days.

Thanks,
Alexis

Nov 12 '05 #2

P: n/a
You are so far correct in the way you described it.
Now I had to modified the classes to add namepaces to it.

now the Output class is annotated with
[System.Xml.Serialization.XmlRootAttribute(Namespac e="mymessagenamespace",
IsNullable=false)]

but the xml document still wil not have the namespaces

/Alexis
Nov 12 '05 #3

P: n/a
I don't understand why you are adding namespaces to the class if the actual
document will not have namespaces.

Maybe you can look into the attribute overrides that are possible in XML
Serialization - perhaps that will do what you want.
-D

"Alexis" <is******@yahoo.com> wrote in message
news:b2**************************@posting.google.c om...
You are so far correct in the way you described it.
Now I had to modified the classes to add namepaces to it.

now the Output class is annotated with
[System.Xml.Serialization.XmlRootAttribute(Namespac e="mymessagenamespace",
IsNullable=false)]

but the xml document still wil not have the namespaces

/Alexis

Nov 12 '05 #4

P: n/a
Dino that is what I did in the first place and there is the problem.
Look at the first posting.

"Dino Chiesa [Microsoft]" <di****@online.microsoft.com> wrote in message news:<#M**************@TK2MSFTNGP11.phx.gbl>...
I don't understand why you are adding namespaces to the class if the actual
document will not have namespaces.

Maybe you can look into the attribute overrides that are possible in XML
Serialization - perhaps that will do what you want.
-D

"Alexis" <is******@yahoo.com> wrote in message
news:b2**************************@posting.google.c om...
You are so far correct in the way you described it.
Now I had to modified the classes to add namepaces to it.

now the Output class is annotated with
[System.Xml.Serialization.XmlRootAttribute(Namespac e="mymessagenamespace",
IsNullable=false)]

but the xml document still wil not have the namespaces

/Alexis

Nov 12 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.