Stefan Meyer Stefan Meyer - 7 months ago 59
Vb.net Question

Deserializing (Universal App) XML serialized with vb.net

I am serializing a class (PSD) using vb.net in a Windows Forms Application:

Dim objStreamWriter As New System.IO.StreamWriter("e:\test.xml")
Dim x As New System.Xml.Serialization.XmlSerializer(PSD.GetType)
x.Serialize(objStreamWriter, PSD)
objStreamWriter.Close()


I need to deserialze that class in a Windows Universal App.
This is my code so far:

Dim picker As Windows.Storage.Pickers.FileOpenPicker = New Windows.Storage.Pickers.FileOpenPicker
picker.ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail
picker.FileTypeFilter.Add(".xml")
Dim File As Windows.Storage.StorageFile = Await picker.PickSingleFileAsync
Dim Fl = Await File.OpenAsync(Windows.Storage.FileAccessMode.Read)
Dim inStream As Stream = Fl.AsStreamForRead()
Dim PSD As New PlatinaStammdaten
Dim serializer = New XmlSerializer(GetType(List(Of PlatinaStammdaten)), New Type() {GetType(PlatinaStammdaten)})
PSD = serializer.Deserialize(inStream)


What I get is the error "There is an error in XML document (2, 2)."

This is the beginning of the generated XML-file:

<?xml version="1.0" encoding="utf-8"?>
<Stammdaten xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Lose>
<Los>
<Name>Sonstige</Name>
<KZ>000</KZ>
</Los>
<Los>
<Name>LOS 1; Allgemein / Planung</Name>
<KZ>110</KZ>
</Los>
...


What error is in my code (writing or reading) that leads to that error?

Answer

I got it working now. This is my deserializing code now:

Dim picker As Windows.Storage.Pickers.FileOpenPicker = New Windows.Storage.Pickers.FileOpenPicker
        picker.FileTypeFilter.Add(".xml")
        Dim File As Windows.Storage.StorageFile = Await picker.PickSingleFileAsync
        Dim Fl = Await File.OpenAsync(Windows.Storage.FileAccessMode.Read)
        Dim FileZiel As Windows.Storage.StorageFile = Await File.CopyAsync(FolderZiel, File.Name, Windows.Storage.NameCollisionOption.ReplaceExisting)
        Dim Fl2 = Await FileZiel.OpenAsync(Windows.Storage.FileAccessMode.ReadWrite)
        Dim inStream2 As Stream = Fl2.AsStreamForRead()
        Dim sr As StreamReader = New StreamReader(inStream2)
        Dim PSD As New PlatinaStammdaten
        Dim x As New XmlSerializer(PSD.GetType)
        PSD = x.Deserialize(sr)
        sr.Dispose()