将VB.Net Xml反序列化为类

将VB.Net Xml反序列化为类,xml,vb.net,deserialization,xml-deserialization,Xml,Vb.net,Deserialization,Xml Deserialization,我在尝试将一些XML反序列化到我创建的类中时遇到了一些问题 我得到的错误是: 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.XmlS

我在尝试将一些XML反序列化到我创建的类中时遇到了一些问题

我得到的错误是:

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
就我个人而言,我无法理解为什么它不会反序列化。我的应用程序中有其他不同类的实例,我已经检查过,它们的结构是相同的,所以我不明白为什么它不起作用


我需要另一双眼睛来检查我做了什么,有人有什么建议吗?

正如Chris在评论中提到的,用于
列表(文档)
的根元素名称必须是
数组文档
(单数),因此如果XML包含
数组文档
(复数),它将不会自动工作作为根元素

如果您需要按原样反序列化,一个简单的解决方案是创建一个
ArrayOfDocuments
类并反序列化到该类中,而不是反序列化到
列表(文档)

公共类文档
公共属性类型为字符串
公共属性文件名为字符串
末级
公共类排列文档
公共财产文件作为文件()
末级
然后:

Dim xml作为新的XmlSerializer(GetType(ArrayOfDocuments))
foundDocuments=TryCast(xml.Deserialize(currentStringReader),ArrayOfDocuments)

您可以通过复制xml文本,然后在visual studio中自动从xml生成类:

编辑>>粘贴特殊>>将XML粘贴为类

我这样做了,它产生了课程

_
部分公开类排列文档
私有文档字段()作为ArrayOfDocumentsDocument
_
公共财产文档()作为ArrayOfDocumentsDocument()
得到
Return Me.documentField
结束
设置(值为ArrayOfDocumentsDocument())
Me.documentField=值
端集
端属性
末级
_
部分公共类数组文档文档
私有类型字段作为字符串
私有文件名字段作为字符串
作为字符串的公共属性类型()
得到
返回我.typeField
结束
设置(值为字符串)
Me.typeField=值
端集
端属性
作为字符串的公共属性FileName()
得到
Return Me.fileNameField
结束
设置(值为字符串)
Me.fileNameField=值
端集
端属性
末级
(将自动名称
ArrayOfDocumentDocument
手动更改为
Document

这很容易反序列化

Imports System.Xml.Serialization
导入System.IO

Dim s作为新的XmlSerializer(GetType(ArrayOfDocuments))
将m设置为阵列文档
使用sr作为新的StreamReader(“XMLFile1.xml”)
m=s.反序列化(sr)
终端使用
Dim foundDocuments=m.Document.ToList()

向克里斯·杜纳韦大声呼喊,他在上面的一条评论中为我解决了这个问题

是更改ArrayOfDocuments=>ArrayOfDocuments的简单案例


在那之后,它工作得很好

我也看不出有什么马上就错了。但是试着换一种方式。创建
文档的列表
s,然后将其序列化并比较生成的xml。那可能会让你知道什么是错的现在我想起来了,您的xml的根不应该是
(单数)吗?伙计,我真不敢相信这就是问题所在。有时候,只需要多看一眼就可以看出你的错误!哇,这是一个很棒的提示,我甚至不知道Visual Studio中有这个提示,谢谢
<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