2

The xml file should like this after I serialize and then I want to deserialize this in vb.net. I am a begginer in programming. Any help is appreciated.

<?xml version="1.0"?>
<Countries>
  <Country>
    <CID>1</CID>
    <CountryName>India</CountryName>
    <States>
      <State> New Delhi </State>
      <State> Maharashtra </State>
      <State> Rajasthan </State>
    </States>
  </Country>
  <Country>
    <CID>2</CID>
    <CountryName>United States</CountryName>
    <States>
      <State> Washington </State>
      <State> Texas </State>
    </States>
  </Country>
  <Country>
    <CID>3</CID>
    <CountryName>Australia</CountryName>
    <States>
      <State> Queensland </State>
      <State> Victoria </State>
    </States>
  </Country>
</Countries>
Victor Zakharov
  • 25,801
  • 18
  • 85
  • 151
subhrendu
  • 147
  • 1
  • 2
  • 19
  • You should start here http://msdn.microsoft.com/library/58a18dwa(v=vs.90).aspx?cs-save-lang=1&cs-lang=vb – Perfect28 Jun 16 '14 at 14:29
  • is the purpose to save some data and reload or to export the data to a given file layout? – Ňɏssa Pøngjǣrdenlarp Jun 16 '14 at 14:46
  • Do you have a data structure that needs to be serialized like this? Please share some VB.NET code. Do you need to serialize and also deserialize this XML? – Victor Zakharov Jun 16 '14 at 15:10
  • @Neolisk i need a class structure that will be serialized into the above xml file and deserialize the xml to get back the original class structure. i am finding it difficult to get started – subhrendu Jun 17 '14 at 05:31
  • @subhrendu Look at my answer, it should get you started... – Styxxy Jun 17 '14 at 08:51
  • @Styxxy i also need the code for serializing into the xml file. xml file should look like d one i hve shown above. i hope you understand – subhrendu Jun 17 '14 at 09:08
  • @subhrendu I added the code for serialization. But really, you should try to find it yourself next time. It is all quite easy to find on the internet, by searching and/or reading the documentation. – Styxxy Jun 17 '14 at 09:26
  • @styxxy thanks a lot. it works. will be sure to do next time – subhrendu Jun 17 '14 at 10:26
  • @Styxxy offcourse i will mark it as an answer. just want to know if i wanted to add multiple state names under the same country tag what would i do. – subhrendu Jun 17 '14 at 13:22
  • @subhrendu Currently it is a list of strings, but you can change it to any type you want (and make sure that type can be serialized as well). You can make a `State` class, or it is possible to use an `Enum`. The answer is just a base, now you can apply the same technique to another class, or your own class definition(s). – Styxxy Jun 17 '14 at 14:56

1 Answers1

7

I'd advise you to definitely look into XML serialization. A lot of information can be found on MSDN (but also using any search engine). For example on MSDN: Introducing XML Serialization.

If you have nothing yet, for code. I would keep it very simple to deserialize the given XML structure. You can create a simple class definition for a Country, as shown below:

Public Class Country
    Public Property CID As Integer
    Public Property CountryName As String
    Public Property States As List(Of String)

    Public Sub New()
        States = New List(Of String)()
    End Sub
End Class

Now this doesn't work yet 100%. You have to help the serialization object with the list of states. You can annotate (with attributes) the States, so the serializer knows that each item is named differently (default it would be <string>item</string>). You can use the XmlArrayItem attribute for this.

<Serializable()>
Public Class Country
    Public Property CID As Integer
    Public Property CountryName As String
    <XmlArrayItem("State")>
    Public Property States As List(Of String)

    Public Sub New()
        States = New List(Of String)()
    End Sub
End Class

Finally, for deserialization. I'd deserialize to a List(Of Country), as it clearly is a list. (Assuming the above XML is stored in a file "obj.xml".)

Dim serializer As New XmlSerializer(GetType(List(Of Country)))
Dim deserialized As List(Of Country) = Nothing
Using file = System.IO.File.OpenRead("obj.xml")
    deserialized = DirectCast(serializer.Deserialize(file), List(Of Country))
End Using

Now we still have to help the serializer object, because otherwise it doesn't know how to deserialize the given XML; as it doesn't determine the root node correctly. We can use an overload of the constructor here, in which we can say what the root node is (XmlSerializer Constructor (Type, XmlRootAttribute)).

Final code for deserialization would be:

Dim serializer As New XmlSerializer(GetType(List(Of Country)), New XmlRootAttribute("Countries"))
Dim deserialized As List(Of Country) = Nothing
Using file = System.IO.File.OpenRead("obj.xml")
    deserialized = DirectCast(serializer.Deserialize(file), List(Of Country))
End Using

Code for serialization (writing to file "obj.xml"):

Dim countries As New List(Of Country)()
' Make sure you add the countries to the list

Dim serializer As New XmlSerializer(GetType(List(Of Country)), New XmlRootAttribute("Countries"))
Using file As System.IO.FileStream = System.IO.File.Open("obj.xml", IO.FileMode.OpenOrCreate, IO.FileAccess.Write)
    serializer.Serialize(file, countries)
End Using

All this could have been found quite easily by searching and reading the documentation.

Styxxy
  • 7,462
  • 3
  • 40
  • 45