CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Dec 2020
    Posts
    2

    Question No output displayed in control after deserializing XML to List of Objects

    Hi folks,

    I am developing a Windows Phone application for a school exercise.
    The issue is that the obtained list of object (class Persoon) is not displayed in the control when I use an index (or .First() for example), but the code does not return any errors. The weird thing is that when I don't use indexation or when I use .Take(1) the List objects are displayed in the control .
    Can someone help me out, what am I doing wrong?

    Class Persoon:

    Code:
    <XmlRoot(ElementName:="Adresboek")>
        Public Class Persoon
    
        Private _achternaam As String
        Private _voornaam As String
        Private _emailadres As String
    
    
        <XmlElement(ElementName:="Achternaam")>
        Public Property Achternaam() As String
            Get
                Return _achternaam
            End Get
            Set(ByVal value As String)
                _achternaam = value
            End Set
        End Property
    
        <XmlElement(ElementName:="Voornaam")>
        Public Property Voornaam() As String
            Get
                Return _voornaam
            End Get
            Set(ByVal value As String)
                _voornaam = value
            End Set
        End Property
    
        <XmlElement(ElementName:="Emailadres")>
        Public Property Emailadres() As String
            Get
                Return _emailadres
            End Get
            Set(ByVal value As String)
                _emailadres = value
            End Set
        End Property
    
    
    End Class
    The Mainpage class:

    Code:
    Partial Public Class MainPage
        Inherits PhoneApplicationPage
    
    
        ' Constructor
        Public Sub New()
            InitializeComponent()
    
            SupportedOrientations = SupportedPageOrientation.Portrait Or SupportedPageOrientation.Landscape
    
    
    
            'Write default XML data to XML-file
            Dim xmlWriterSettings As XmlWriterSettings = New XmlWriterSettings()
            xmlWriterSettings.Indent = True
    
            Using myIsolatedStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
    
                Using writeStream As IsolatedStorageFileStream = myIsolatedStorage.OpenFile("Contacten.xml", FileMode.Create, FileAccess.ReadWrite, FileShare.ReadWrite)
                    Dim serializer As XmlSerializer = New XmlSerializer(GetType(List(Of Persoon)))
    
                    Using xmlWriter As XmlWriter = xmlWriter.Create(writeStream, xmlWriterSettings)
                        serializer.Serialize(xmlWriter, GeneratePersonData())
                    End Using
                End Using
            End Using
    
            'Read and sort XML data by Voornaam to Object Persoon and show first item in list
            Try
    
                Using myIsolatedStorage As IsolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()
    
                    Using readStream As IsolatedStorageFileStream = myIsolatedStorage.OpenFile("Contacten.xml", FileMode.Open)
                        Dim serializer As XmlSerializer = New XmlSerializer(GetType(List(Of Persoon)))
    
                        Dim data As List(Of Persoon) = CType(serializer.Deserialize(readStream), List(Of Persoon))
                        Dim dataSorted = data.OrderBy(Function(x) x.Voornaam).ToList
                        Dim contactList As New List(Of Persoon)
                        contactList = dataSorted
                        Dim first = contactList(0)
                        ContactListBox.ItemsSource = first
                    End Using
                End Using
    
            Catch
            End Try
    
    
    
            'Dim path As String = "Assets\Contacten.xml"
            'Dim serializer = New XmlSerializer(GetType(Adresboek))
            'Dim streamReader = New FileStream(path, FileMode.Open)
            'Dim readContainer = TryCast(serializer.Deserialize(streamReader), Adresboek)
            'ContactListBox.DataContext = readContainer
            'streamReader.Close()
    
        End Sub
    
        'Generate default list Persoon and add items to list
        Private Function GeneratePersonData() As List(Of Persoon)
            Dim data As List(Of Persoon) = New List(Of Persoon)()
            data.Add(New Persoon() With {
                .Achternaam = "van Gaal",
                .Voornaam = "Louis",
                .Emailadres = "louisvangaal@prive.nl"
            })
            data.Add(New Persoon() With {
                .Achternaam = "Kluivert",
                .Voornaam = "Patrick",
                .Emailadres = "patrickkluivert@prive.nl"
            })
            data.Add(New Persoon() With {
                .Achternaam = "van Persie",
                .Voornaam = "Robin",
                .Emailadres = "robinvanpersie@prive.nl"
            })
            data.Add(New Persoon() With {
                .Achternaam = "Robben",
                .Voornaam = "Arjen",
                .Emailadres = "arjenrobben@prive.nl"
            })
            Return data
        End Function
    
    End Class

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: No output displayed in control after deserializing XML to List of Objects

    You have to debug your code step-by-step to find out what, where and why goes wrong or non expected.
    Victor Nijegorodov

  3. #3
    Join Date
    Dec 2020
    Posts
    2

    Re: No output displayed in control after deserializing XML to List of Objects

    Quote Originally Posted by VictorN View Post
    You have to debug your code step-by-step to find out what, where and why goes wrong or non expected.
    I did and I found out already, the Datacontext from the Listbox control can only hold List objects no single objects.
    So I used Textblock instead of Datacontext, to hold the single object.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured