469,338 Members | 8,430 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,338 developers. It's quick & easy.

How to tell the XmlSerializer to ignore all namespaces

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

Similar topics

4 posts views Thread by Maersa | last post: by
5 posts views Thread by Alexis | last post: by
3 posts views Thread by Loui Mercieca | last post: by
reply views Thread by =?Utf-8?B?QW1hemVkc2FpbnQ=?= | last post: by
9 posts views Thread by =?Utf-8?B?ai5hLiBoYXJyaW1hbg==?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.