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

Problems with XmlSerializer and namespaces

P: n/a
I have an object, Teacher, that has a namespace. Let's call it http://foo.
Teacher has a child object called Name, composed of String first, middle,
last, etc. Problem I have is when I try to deserialize it. When I
deserialize a single Teacher, it works when I put the namespace on the
Teacher element. However, if I have just the namespace on Teacher, it will
deserialize fine as Teacher. *However*, if I try to deserialize an
ArrayOfTeacher, Teacher.Name doesn't deserialize (it's null); without the
array, it's OK. What I wound up doing was serializing both a Teacher and an
ArrayOfTeacher and found that when it's just TEacher, the serializer puts
the xsd and xsi namespaces on the array & put the http://foo namespace *on
the Name element only*. If I put that same namespace on both Teacher *and*
Name, it comes out OK, except when deserializing an ArrayOfTeacher.
So basically, my problem is: where do I put the namespaces for consistent
deserialization? If the object to be deserialized is in an array, only its
children can have the namespace declared on it, but not vice versa, so I
have no consistent way to represent the Teacher as XML and have it
deserialize both as a single entity and an array of them.
Feb 17 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a
BTW: This is the properly serialized version of the array and the single
object, to show the difference in where the namespace is placed; I have not
found a way to deserialize both forms with the same namespace structure:

<!-- Puts namespace on inner-most child -->
<?xml version="1.0" encoding="utf-16"?>
<ArrayOfTeacher xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Teacher Ssn="123-34-3425">
<Name ID="0" LastName="Patrick" FirstName="Richard" MiddleName="K"
Type="01" xmlns="http://www.sifinfo.org/infrastructure/1.x" />
</Teacher>
</ArrayOfTeacher>
<!-- Puts namespace on overall parent (this one actually makes sense to
me...the previous one does not) -->
<?xml version="1.0" encoding="utf-16"?>
<Teacher xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" Ssn="123-34-3425"
xmlns="http://www.sifinfo.org/infrastructure/1.x">
<Name ID="0" LastName="Patrick" FirstName="Richard" MiddleName="K"
Type="01" />
</Teacher>
Feb 17 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.