我在Windows窗体应用程序中使用vb.net序列化一个类(PSD):
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()
我需要在Windows Universal App中反序列化该类。 到目前为止,这是我的代码:
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)
我得到的是错误“XML文档中存在错误(2,2)。”
这是生成的XML文件的开头:
<?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>
...
导致该错误的代码(写入或读取)中出现了什么错误?
答案 0 :(得分:1)
我现在就开始工作了。 这是我现在的反序列化代码:
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()
答案 1 :(得分:0)
看起来您尝试将列表反序列化为单个实例
Dim PSD作为新的PlatinaStammdaten Dim serializer = New XmlSerializer(GetType(List(Of PlatinaStammdaten)),New Type(){GetType(PlatinaStammdaten)}) PSD = serializer.Deserialize(inStream)
尝试将PSD更改为
Dim PSD As List(of PlatinaStammdaten)
因为我们无法看到您将xml放入其中的完整xml或类很难判断您是否会遇到任何其他错误
答案 2 :(得分:0)
以下代码可以使用
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>