473,508 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

XmlSerializer.Deserialize complain when root declare the namespace

Subject: XmlSerializer.Deserialize complain when root declare the namespace

If I do XmlSerializer.Deserialize( myString ) and myString is:
String myString = "<?xml version=\"1.0\"
encoding=\"utf-16\"?><DocumentResponse ><documentSize

xmlns=\"urn:webservices.docharbor.com\">23</documentSize></DocumentResponse>";

It's working

But if myString is:
String myString = "<?xml version=\"1.0\"
encoding=\"utf-16\"?><DocumentResponse

xmlns=\"urn:webservices.docharbor.com\"><documentS ize
23</documentSize></DocumentResponse>";


It's not working!

XmlDocument is also part of the problem because when I use XmlDocument and I
put a namespace to every

node:
XmlElement children =
document.CreateElement(soapNode.Name,DOCHARBOR_TYP E_URN);

It will remove the namespace to all children except the root when I do this:
xml.WriteTo(new

XMLTextWriter(...))

The declaration of DocumentResponse look like this:
[System.Xml.Serialization.XmlTypeAttribute(Namespac e="urn:webservices.docharbor.com")]
public class DocumentResponse {

/// <remarks/>
public int documentSize;
}
1- Please confirm that XmlSerializer.Deserialize has a bug.
2- Please give me a good workaround.
3- Let me know if the bug will be inserted in your bug tracker.
Nov 12 '05 #1
4 11787
Hi,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that when you have put the namespace
defination in root element, deserialize process cannot be done. If there is
any misunderstanding, please feel free to let me know.

I have checked the xml string on my machine. I noticed that the class
DocumentResponse has a default namespace "urn:webservices.docharbor.com".
So, when we are doing deserialization, we also have to set the default
namespace when initializing the serializer. Here I have written a code
snippet. It works fine on my computer.

String myString = "<?xml version=\"1.0\"
encoding=\"utf-16\"?><DocumentResponse
xmlns=\"urn:webservices.docharbor.com\"><documentS ize>23</documentSize></Doc
umentResponse>";
XmlSerializer s = new XmlSerializer(typeof(DocumentResponse),
"urn:webservices.docharbor.com");
StringReader r = new StringReader(myString);
DocumentResponse dr = (DocumentResponse)s.Deserialize(r);

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 12 '05 #2
Adding a namespace to the constructor fix my problem. It still weird that it
was working if the namespace where not put in the root element but to its
children without having to put a namespace in the constructor.

Thanks.

"Kevin Yu [MSFT]" wrote:
Hi,

First of all, I would like to confirm my understanding of your issue. From
your description, I understand that when you have put the namespace
defination in root element, deserialize process cannot be done. If there is
any misunderstanding, please feel free to let me know.

I have checked the xml string on my machine. I noticed that the class
DocumentResponse has a default namespace "urn:webservices.docharbor.com".
So, when we are doing deserialization, we also have to set the default
namespace when initializing the serializer. Here I have written a code
snippet. It works fine on my computer.

String myString = "<?xml version=\"1.0\"
encoding=\"utf-16\"?><DocumentResponse
xmlns=\"urn:webservices.docharbor.com\"><documentS ize>23</documentSize></Doc
umentResponse>";
XmlSerializer s = new XmlSerializer(typeof(DocumentResponse),
"urn:webservices.docharbor.com");
StringReader r = new StringReader(myString);
DocumentResponse dr = (DocumentResponse)s.Deserialize(r);

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 12 '05 #3
Hi,

The parameter when initializing the XmlSerializer sets the default
namespace. Since the namespace is in root element, the default namespace is
required. Because if you don't set that, the serializer will look into
another namespace and the element will not be found.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."

Nov 12 '05 #4

Sebastien,

The namespace is an integral part of the name of an XML element or
attribute. It's like first name and last name with people. There are
many, many Sebastiens out there, so in order to make sure I ask for you
I have to ask for Sebastien Tardif. In XML you have to ask for an
element by name and namespace, which is what the XmlSerializer does.
It's looking for a match of element name AND namespace.

HTH,
Christoph Schittko
MVP XML
http://weblogs.asp.net/cschittko
-----Original Message-----
From: Sebastien Tardif [mailto:se**************@community.nospam]
Posted At: Friday, November 05, 2004 7:39 AM
Posted To: microsoft.public.dotnet.xml
Conversation: XmlSerializer.Deserialize complain when root declare the
namespace
Subject: RE: XmlSerializer.Deserialize complain when root declare the
names

Adding a namespace to the constructor fix my problem. It still weird that it
was working if the namespace where not put in the root element but to its children without having to put a namespace in the constructor.

Thanks.

"Kevin Yu [MSFT]" wrote:
Hi,

First of all, I would like to confirm my understanding of your issue.
From
your description, I understand that when you have put the namespace
defination in root element, deserialize process cannot be done. If
there is
any misunderstanding, please feel free to let me know.

I have checked the xml string on my machine. I noticed that the
class DocumentResponse has a default namespace

"urn:webservices.docharbor.com".
So, when we are doing deserialization, we also have to set the default namespace when initializing the serializer. Here I have written a code snippet. It works fine on my computer.

String myString = "<?xml version=\"1.0\"
encoding=\"utf-16\"?><DocumentResponse

xmlns=\"urn:webservices.docharbor.com\"><documentS ize>23</documentSize><
/D oc
umentResponse>";
XmlSerializer s = new XmlSerializer(typeof(DocumentResponse),
"urn:webservices.docharbor.com");
StringReader r = new StringReader(myString);
DocumentResponse dr = (DocumentResponse)s.Deserialize(r);

HTH.

Kevin Yu
=======
"This posting is provided "AS IS" with no warranties, and confers no
rights."


Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1608
by: Zion Zadik | last post by:
Dear all, I have a set of c# data classes which i need to fill their data from xml files. serialization looks to be the best way to accomplish this task. Since the data classes are compiled and...
5
5407
by: Stuart Robertson | last post by:
I am trying to find a solution that will allow me to use XmlSerializer to serialize/deserialize a collection of objects where a given object is shared between two or more other objects, and not...
8
3542
by: Harris Boyce | last post by:
Hello, I'm trying to use the FOR XML EXPLICIT clause with SQL Server to deserialize data from my database into a strongly-typed collection object that I will use throughout my application. I...
16
9491
by: Bob Rock | last post by:
Hello, when serializing an array of elements of a class Classname using XmlSerializer.Serialize() I get an XML like the following: <?xml version="1.0"> <ArrayOfClassname> ....... ..........
2
942
by: magister | last post by:
Hello I got this working but it is not how I really want it, basically I have an xml file which has a root of <test> and can be filled with 3 different types of <question> elements with different...
3
4488
by: Loui Mercieca | last post by:
Hi, I have created a class, named FormField , which basically contains two fields, name and value. I have set the tag before the class and the field is set as an XmlAttribute whil the name as...
1
5108
by: Yewen Tang | last post by:
I have a schema file datamodel.xsd, element "properties" is declared as a type of "baseProperty". The schema file also defines "derivedProperty" is a derived type of "baseProperty". <?xml...
0
2007
by: PSingh | last post by:
Hi, I know this is a frequently asked question but have tried several combinations and cant seem to figure this out. I am serializing my object as follows: XmlSerializer serializer = new...
0
2786
by: theonlydavewilliams | last post by:
Hi there I'm hoping there's an easy answer to a (hopefully) not too long-winded issue... I'm building a C# web client using a proxy wsdl.exe'd from a wsdl file and six schemas, each in a different...
0
7233
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7342
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
7410
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
7505
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5650
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5060
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3201
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
774
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.