By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,996 Members | 1,179 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,996 IT Pros & Developers. It's quick & easy.

XmlSerializer.Deserialize complain when root declare the namespace

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

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 discussion thread is closed

Replies have been disabled for this discussion.