VB.Net Xml Desearialization到一个类
我有一个问题试图将一些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
我不能,为了我的生活,找出为什么它不会反序列化。我的应用程序中有不同类的其他实例,我已经检查过,它们的结构方式是相同的,所以我不明白为什么它不起作用。
我需要另一双眼睛来检查我做了什么,有没有人有任何建议?
喊出克里斯·达纳韦在谁上述评论的一个解决了这个问题对我来说。
正在改变ArrayOfDocuments的简单情况=> ArrayOfDocument
之后,它完美地工作
正如在评论中提到由克里斯,用于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)
您可以通过复制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()
哇,这是一个很棒的提示,我从来不知道在Visual Studio中有,谢谢 – user3489088
我看不出有什么不对,立即要么。但尝试另一种方式。创建一个'Document'列表,然后序列化它并比较生成的xml。这可能会让你了解什么是错的。 - 现在我想到了,xml的根不应该是“”(单数)吗? –
儿子的一个.....现货队友,不能相信它就是这样。有时只需要额外的眼睛就可以看到你的错误! – user3489088