2017-07-18 44 views
1

我有一個問題試圖將一些XML反序列化成我創建的類。VB.Net Xml Desearialization到一個類

我得到的錯誤是:

There is an error in XML document (1, 2). 

    at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events) 
    at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader) 
    at CommonLayer.InvuManager.FindDocuments(String policy, String year) in C:\GIT\novus\CommonLayer\InvuManager.vb:line 194 
    at Novus.NavigationControlRisk.UpdateInvuDocumentsFolderTitle(TreeListNode& documentsFolderNode, String policy, String year) in C:\GIT\novus\Dashboard\src\Dashboard\NavigationControls\NavigationControlRisk.vb:line 3125 
    at Novus.NavigationControlRisk.PopulateFolders(TreeListNode parentNode, Boolean isAttachingPolicy, Boolean refreshData) in C:\GIT\novus\Dashboard\src\Dashboard\NavigationControls\NavigationControlRisk.vb:line 1280 
    at Novus.NavigationControlRisk.PopulateNode(Boolean refreshData) in C:\GIT\novus\Dashboard\src\Dashboard\NavigationControls\NavigationControlRisk.vb:line 1158 
    at Novus.NavigationControlRisk.mainTreeList_MouseClick(Object sender, MouseEventArgs e, Boolean refreshData) in C:\GIT\novus\Dashboard\src\Dashboard\NavigationControls\NavigationControlRisk.vb:line 2340 
    at Novus.NavigationControlRisk._Lambda$__R25-1(Object a0, MouseEventArgs a1) 
    at System.Windows.Forms.Control.OnMouseClick(MouseEventArgs e) 
    at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks) 
    at System.Windows.Forms.Control.WndProc(Message& m) 
    at DevExpress.XtraEditors.Container.EditorContainer.WndProc(Message& m) 
    at DevExpress.XtraTreeList.TreeList.WndProc(Message& m) 
    at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 
    at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg) 
    at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData) 
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context) 
    at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context) 
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun() 
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel() 
    at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine) 
    at Novus.My.MyApplication.Main(String[] Args) in :line 81 
    at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args) 
    at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 
    at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 
    at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx) 
    at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 
    at System.Threading.ThreadHelper.ThreadStart() 

這裏是我創建的類,它沒有什麼特別的,在這一點上,我只是試圖得到它的工作:

Imports System.Xml.Serialization 

<Serializable, XmlRoot("Document")> _ 
Public Class Document 
    <XmlElement("Type")> _ 
    Public Property Type As String 
    <XmlElement("FileName")> _ 
    Public Property FileName As String 
End Class 

而且這裏是我使用的文件的XML:

<ArrayOfDocuments> 
    <Document> 
    <Type>Debit/Credit note</Type> 
    <FileName>dbE12901_acc1.doc</FileName> 
    </Document> 
    <Document> 
    <Type>Generic</Type> 
    <FileName>a3_lmbc_categories.xls</FileName> 
    </Document> 
</ArrayOfDocuments> 

最後,這裏是我使用的代碼:

Dim foundDocuments As New List(Of Document) 
Dim xmldoc As New XmlDocument 
xmldoc.Load(InterfaceFilePath) 
Dim allText As String = xmldoc.InnerXml 

Using currentStringReader As New StringReader(allText) 
    Dim xml as New XmlSerializer(GetType(List(Of Document))) 
    foundDocuments = TryCast(xml.Deserialize(currentStringReader), List(Of Document)) 
End Using 

我不能,爲了我的生活,找出爲什麼它不會反序列化。我的應用程序中有不同類的其他實例,我已經檢查過,它們的結構方式是相同的,所以我不明白爲什麼它不起作用。

我需要另一雙眼睛來檢查我做了什麼,有沒有人有任何建議?

+2

我看不出有什麼不對,立即要麼。但嘗試另一種方式。創建一個'Document'列表,然後序列化它並比較生成的xml。這可能會讓你瞭解什麼是錯的。 - 現在我想到了,xml的根不應該是「」(單數)嗎? –

+1

兒子的一個.....現貨隊友,不能相信它就是這樣。有時只需要額外的眼睛就可以看到你的錯誤! – user3489088

回答

1

喊出克里斯·達納韋在誰上述評論的一個解決了這個問題對我來說。

正在改變ArrayOfDocuments的簡單情況=> ArrayOfDocument

之後,它完美地工作

0

正如在評論中提到由克里斯,用於List(Of Document)根元素名稱需要ArrayOfDocument(單數),所以如果XML包含ArrayOfDocuments(複數)的根元素,它不會自動運行。

如果您需要反序列化,因爲,一個簡單的解決方案是創建一個ArrayOfDocuments類和反序列化到,與其反序列化到List(Of Document)

Public Class Document 
    Public Property Type As String 
    Public Property FileName As String 
End Class 

Public Class ArrayOfDocuments 
    <XmlElement> 
    Public Property Document As Document() 
End Class 

然後:

Dim xml As New XmlSerializer(GetType(ArrayOfDocuments)) 
foundDocuments = TryCast(xml.Deserialize(currentStringReader), ArrayOfDocuments) 
3

您可以通過複製xml文本然後在Visual Studio中自動從xml生成類:

編輯>>選擇性粘貼>>粘貼XML作爲類

我做到這一點,它產生的類

<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True), _ 
System.Xml.Serialization.XmlRootAttribute([Namespace]:="", IsNullable:=False)> _ 
Partial Public Class ArrayOfDocuments 
    Private documentField() As ArrayOfDocumentsDocument 
    <System.Xml.Serialization.XmlElementAttribute("Document")> _ 
    Public Property Document() As ArrayOfDocumentsDocument() 
     Get 
      Return Me.documentField 
     End Get 
     Set(value As ArrayOfDocumentsDocument()) 
      Me.documentField = value 
     End Set 
    End Property 
End Class 

<System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True)> _ 
Partial Public Class ArrayOfDocumentsDocument 
    Private typeField As String 
    Private fileNameField As String 
    Public Property Type() As String 
     Get 
      Return Me.typeField 
     End Get 
     Set(value As String) 
      Me.typeField = value 
     End Set 
    End Property 
    Public Property FileName() As String 
     Get 
      Return Me.fileNameField 
     End Get 
     Set(value As String) 
      Me.fileNameField = value 
     End Set 
    End Property 
End Class 

(自動變更名稱ArrayOfDocumentDocument手動Document

這是反序列化很容易

Imports System.Xml.Serialization 
Imports System.IO 

Dim s As New XmlSerializer(GetType(ArrayOfDocuments)) 
Dim m As ArrayOfDocuments 
Using sr As New StreamReader("XMLFile1.xml") 
    m = s.Deserialize(sr) 
End Using 
Dim foundDocuments = m.Document.ToList() 

enter image description here

+1

哇,這是一個很棒的提示,我從來不知道在Visual Studio中有,謝謝 – user3489088