VB.Net Xml反序列化为一类 [英] VB.Net Xml Deserialization into a Class

查看:91
本文介绍了VB.Net Xml反序列化为一类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将一些XML反序列化为我创建的类时遇到一个问题。

I have a bit of an issue trying to deserialize some XML into a class that I have created.

我得到的错误是:

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()

这是我创建的类,在这一点上没有什么幻想,我只是尝试使其正常工作:

Here is the class I created, it's nothing fancy at this point, I'm simply trying to get it to work:

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从我正在使用的文件中获取:

And here is the XML from the file I am using:

<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>

最后,这是我使用的代码:

Finally, here is the code I am using:

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

我终生不能对我来说,弄清楚为什么它不会反序列化。我的应用程序中还有其他不同类的实例,我已经检查过了,它们的结构方式是相同的,所以我不明白为什么它不起作用。

I cannot, for the life of me, figure out why it will not deserialize. I have other instances of different classes in my app and I have checked and the way they are structured is the same, so I do not understand why it will not work.

我需要另一只眼睛来检查我的工作,有没有人建议?

I need another pair of eyes just to check what I have done, does anyone have any suggestions?

推荐答案

向Chris Dunaway喊叫谁在上面的评论中为我解决了这个问题。

Shout out to Chris Dunaway who solved it for me in one of the comments above.

一个简单的更改ArrayOfDocuments => ArrayOfDocument

Was a simple case of changing ArrayOfDocuments => ArrayOfDocument

那之后效果很好

这篇关于VB.Net Xml反序列化为一类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆