How do I deserialize a namespaced element to an object in .net?

Posted by pc1oad1etter on Stack Overflow See other posts from Stack Overflow or by pc1oad1etter
Published on 2010-05-07T20:33:17Z Indexed on 2010/05/07 20:38 UTC
Read the original article Hit count: 186

Filed under:
|

Given this XML snippet:

...
<InSide:setHierarchyUpdates>
   <automaticUpdateInterval>5</automaticUpdateInterval>
   <shouldRunAutomaticUpdates>true<shouldRunAutomaticUpdates>
</InSide:setHierarchyUpdates>
...

I am attempting to serialize this object:

Imports System.Xml.Serialization
<XmlRoot(ElementName:="setHierarchyUpdates", namespace:="InSide")> _
Public Class HierarchyUpdate
    <XmlElement(ElementName:="shouldRunAutomaticUpdates")> _
    Public shouldRunAutomaticUpdates As Boolean
    <XmlElement(ElementName:="automaticUpdateInterval")> _
      Public automaticUpdateInterval As Integer
End Class

Like this:

Dim hierarchyUpdater As New HierarchyUpdate
Dim x As New XmlSerializer(hierarchyUpdater.GetType)
Dim objReader As Xml.XmlNodeReader = New Xml.XmlNodeReader(myXMLNode)
hierarchyUpdater = x.Deserialize(objReader)

However, the object, after deserialization, has values of false and zero. If I switch the objReader to a streamreader and read this in as a file, with none of its parents and no namespaces, it works:

<setHierarchyUpdates>
   <automaticUpdateInterval>5</automaticUpdateInterval>
   <shouldRunAutomaticUpdates>true<shouldRunAutomaticUpdates>
</setHierarchyUpdates>

What am I doing wrong? Should I use something other than XMLRoot in the class definition, because, as an XML node, it's not really the root? If so, what? Why are no errors returned when this fails?

© Stack Overflow or respective owner

Related posts about .NET

Related posts about serialization