0

I need to deserialize a XML file in VB.NET.

I'm using the standard XML library. I'm not able to write down the vb code to define the following structure:

<myvar>
    <var>...</var>
    <troublecodes>
        <troublecode>
        ...
        </troublecode>
        <troublecode>
        ....
        </troublecode>
        <statusbyte>
        ....
        </statusbyte>
        <statusbyte>
        ....
        </statusbyte>
        <statusbyte>
        ....
        </statusbyte>
    </troublecodes>
</myvar>

My definitions are:

Public Class MyVar
  <XmlElement("var")> Public name As String
  <XmlElement("troublecodes")> Public troubleCodes As TroubleCodes
End Class

Public Class TroubleCodes
<XmlArrayItem("troublecode")> Public troubleCode() As TroubleCode
<XmlArrayItem("statusbyte")> Public statusByte() As StatusByte
End Class

Public Class TroubleCode
  <XmlElement("one")> Public one As String
  <XmlElement("two")> Public two As String
End Class

Public Class StatusByte
  <XmlElement("three")> Public threeAs String
  <XmlElement("four")> Public four As String
End Class

but the objects are not populated by the deserialization.
How can i define them?

2
  • I changed XmlArrayItem to XmlArray but again no data. Commented Oct 19, 2016 at 14:37
  • Perhaps this'll help: stackoverflow.com/a/20133287/3740093 Commented Oct 19, 2016 at 20:04

2 Answers 2

1

Problems in deserialization can typically be diagnosed by serializing an example of your root type and comparing the generated XML with the desired XML. If I do so with your MyVar type (demo fiddle) I get the following result:

<MyVar>
  <var>my name</var>
  <troublecodes>
    <troubleCode>
      <troublecode>
        <one>one</one>
        <two>two</two>
      </troublecode>
    </troubleCode>
    <statusByte>
      <statusbyte>
        <three>three</three>
        <four>four</four>
      </statusbyte>
    </statusByte>
  </troublecodes>
</MyVar>

This has the following problems:

  • The root node is incorrectly capitalized.

    This can be fixed by adding <XmlRoot("myvar")> to your root type.

  • There is an extra level of nesting of <troubleCode>.

    By default, XmlSerializer serializes all collections including arrays with an outer container element. To suppress the outer container element and serialize the collection as a flat sequence of elements, replace the XmlArrayItem attribute with <XmlElement("troublecode")>.

  • There is also an extra level of nesting of <statusByte>.

Thus your types should be as follows:

<XmlRoot("myvar")> _
Public Class MyVar
    <XmlElement("var")> Public name As String
    <XmlElement("troublecodes")> Public troubleCodes As TroubleCodes
End Class

Public Class TroubleCodes
    <XmlElement("troublecode")> Public troubleCode() As TroubleCode
    <XmlElement("statusbyte")> Public statusByte() As StatusByte
End Class

Public Class TroubleCode
    <XmlElement("one")> Public one As String
    <XmlElement("two")> Public two As String
End Class

Public Class StatusByte
    <XmlElement("three")> Public three As String
    <XmlElement("four")> Public four As String
End Class

Fixed fiddle.

Sign up to request clarification or add additional context in comments.

1 Comment

@SteMMo - I copied your classes verbatim into the fiddle dotnetfiddle.net/qZaw2t, and saw the extra levels. Have you changed the classes after asking the question? And do the classes in the answer generate the XML you require?
1

My solution is:

Public Class TroubleCodes  
    <XmlElement("troublecode")> Public troubleCode() As TroubleCode
    <XmlElement("statusbyte")> Public statusByte() As StatusByte
End Class

Serializing the variable I obtain the same XML code.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.