472,956 Members | 2,604 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,956 software developers and data experts.

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 11744
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
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
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
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
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
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
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
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
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
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...
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.