473,320 Members | 1,691 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 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 9996

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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: ajay.sonawane | last post by:
Hello there You need to create namespace in order to add your own element, But how can I create namespace file? Does it need to post that namespace to some authorized person of RSS? For Example....
0
by: Alexis | last post by:
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...
3
by: Sandy | last post by:
Hi, I have two files as folllows file1.cpp #include<iostream> using namespace std; namespace { void show(); void fun() { cout<<"fun called\n"; } }
6
by: SpOiLeR | last post by:
Why doesn't following code compile? Problem line is commented in code. ---------------------- Cut here------------------------------- #include <iostream> #include <list> #include <string> ...
2
by: tomek.romanowski | last post by:
Hi ! I have problem with validating of the document with multiple namespaces. The odd thing is, that my data work O'K when I test it under XMLSpy but it doesn't work with my C# code. My first...
10
by: anders | last post by:
I have 2 external assemblies A1 and A2 that both define class X in the global namespace. I need to use both assemblies in my VB project but the names X are ambiguous. How can I get around this...
0
by: ryan | last post by:
I've been tasked with consuming a Perl web service that was written by a person in a different department of my company. The problem is it's the guy's first attempt at web services and he doesn't...
0
by: SMart | last post by:
I am having a problem with a C# dll in my Visual Studio 2005 ASP.NET web site project: when I add a reference to a certain the dll, it tells me that the type or namespace “Log” does not exist in the...
5
by: michael.d.pedersen | last post by:
Hello group, I am new to Visual C++ 8 and have a seemingly trivial problem using namespaces. The scenario: I have two classes, TestClass1 and TestClass2, in the namespace Test. I would like to...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.