0
<world> 
    <apac>
        <sasia> 
            <india>
                <sindia>
                    <tn>CHENNAI
                    <tn>Madurai





    <apac>
        <sasia> 
            <india>
                <sindia>
                    <ka>Bangalore
                    <ka>Mysore

When I try 4 level tag its works, but for 5 level tag it doesn't help.

I've tried: trying to serialize and deserialize an xml file using VB.net

  • Maybe [this](https://stackoverflow.com/questions/58686509/trouble-serializing-and-deserializing-multiple-objects/58687570#58687570) will give you some hints. –  Nov 25 '19 at 21:56
  • 1
    Is that really what the xml looks like? You have no closing tags. If so, then xml serialization will not work! If you have a valid xml file, please put a real example in your question, otherwise we can't accurately help you. – djv Nov 25 '19 at 22:27
  • Or [this](https://stackoverflow.com/questions/59074005/saving-listbox-with-additional-information-in-my-settings/59097544#59097544) vb.net version. Still hot. –  Nov 29 '19 at 07:44

1 Answers1

1

Assuming your xml has properly closed tags etc.,

<?xml version="1.0" encoding="utf-8" ?>
<world>
  <apac>
    <sasia>
      <india>
        <sindia>
          <tn>CHENNAI</tn>
          <tn>Madurai</tn>
        </sindia>
      </india>
    </sasia>
  </apac>
  <apac>
    <sasia>
      <india>
        <sindia>
          <ka>Bangalore</ka>
          <ka>Mysore</ka>
        </sindia>
      </india>
    </sasia>
  </apac>
</world>

here are some classes you can use to define your model

<XmlRoot("world")>
Public Class World
    <XmlElement("apac")>
    Public Property Apacs As List(Of Apac)
End Class
Public Class Apac
    <XmlElement("sasia")>
    Public Property Sasia As Sasia
End Class
Public Class Sasia
    <XmlElement("india")>
    Public Property India As India
End Class
Public Class India
    <XmlElement("sindia")>
    Public Property Sindia As Sindia
End Class
Public Class Sindia
    <XmlElement("ka")>
    Public Property Kas As List(Of String)
    <XmlElement("tn")>
    Public Property Tns As List(Of String)
End Class

and code you can use to deserialize

Dim myWorld As World
Dim s As New XmlSerializer(GetType(World))
Using sr As New StreamReader("filename.xml")
    myWorld = DirectCast(s.Deserialize(sr), World)
End Using

and code you can test it with.

Console.WriteLine(myWorld.Apacs.First().Sasia.India.Sindia.Tns.First())
Console.WriteLine(myWorld.Apacs.Last().Sasia.India.Sindia.Kas.First())

CHENNAI
Bangalore

Of course, if you xml doesn't look like this (I must admit I did some guesswork), then this code won't work. If you must, update your question by closing xml tags when required and we can work on the right solution.

djv
  • 15,168
  • 7
  • 48
  • 72