Reputation: 928
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?
Upvotes: 1
Views: 283
Reputation: 928
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()
Upvotes: 1
Reputation: 34433
The code below works
Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO
Module Module1
Const FILENAME As String = "c:\temp\test.xml"
Sub Main()
Dim xs As XmlSerializer = New XmlSerializer(GetType(PlatinaStammdaten))
Dim reader As XmlTextReader = New XmlTextReader(FILENAME)
Dim PSD As PlatinaStammdaten = CType(xs.Deserialize(reader), PlatinaStammdaten)
End Sub
End Module
<XmlRoot("Stammdaten")> _
Public Class PlatinaStammdaten
Private m_lose As Lose
<XmlElement("Lose")> _
Public Property _lose As Lose
Get
Return m_lose
End Get
Set(ByVal value As Lose)
m_lose = value
End Set
End Property
End Class
<Serializable(), XmlRoot("Lose")> _
Public Class Lose
Private m_los As List(Of Los)
<XmlElement("Los")> _
Public Property _los As List(Of Los)
Get
Return m_los
End Get
Set(ByVal value As List(Of Los))
m_los = value
End Set
End Property
End Class
<Serializable(), XmlRoot("Los")> _
Public Class Los
Private m_Name As String
<XmlElement("Name")> _
Public Property name As String
Get
Return m_Name
End Get
Set(ByVal value As String)
m_Name = value
End Set
End Property
Public m_KZ As Integer
<XmlElement("KZ")> _
Public Property KZ As Integer
Get
Return m_KZ
End Get
Set(ByVal value As Integer)
m_KZ = value
End Set
End Property
End Class
'<Lose>
' <Los>
' <Name>Sonstige</Name>
' <KZ>000</KZ>
' </Los>
' <Los>
' <Name>LOS 1; Allgemein / Planung</Name>
' <KZ>110</KZ>
' </Los>
'</Lose>
Upvotes: 0
Reputation: 4156
Looks like you are try to deserialize a list into a single instance
Dim PSD As New PlatinaStammdaten Dim serializer = New XmlSerializer(GetType(List(Of PlatinaStammdaten)), New Type() {GetType(PlatinaStammdaten)}) PSD = serializer.Deserialize(inStream)
Try changing PSD to
Dim PSD As List(of PlatinaStammdaten)
since we cant see the full xml or the class you are putting the xml into it is hard to tell if you will encounter any other errors
Upvotes: 0