473,390 Members | 1,171 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,390 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 10007

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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.