VB.Net Xml 反序列化为类

2023-12-29

我在尝试将一些 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

这是我正在使用的文件中的 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

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

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


您可以通过复制 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 to 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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

VB.Net Xml 反序列化为类 的相关文章

随机推荐

  • Jaxrs 多部分

    我正在尝试向 jaxrs 服务执行请求 该服务的媒体类型设置为multipart form data 该请求包含实体列表 xml 和图像 png 二进制 我已按照中所述创建了请求this https stackoverflow com qu
  • 从派生类对象调用基类方法

    如何从派生类对象调用被派生类重写的基类方法 class Base public void foo cout lt lt base class Derived public Base public void foo cout lt lt de
  • 使用不同的值作为列创建 MySQL 视图

    我搜索了一段时间 找不到我的问题的答案 我有一个看起来像这样的表 date name status 2011 01 01 m1 online 2011 01 01 m2 offline 2011 01 01 m3 online 2011 0
  • 为什么是替换而不是替换!对于红宝石中的字符串?

    replace更改当前字符串而不是返回新实例 为了与 Ruby 中的其他方法保持一致 似乎应该调用replace 这是一个错误 不一致还是我错过了什么 来自 matz 的帖子https www ruby forum com topic 17
  • 如何在gradle中的测试任务中指定@category?

    我想了解是否可以在 gradle 测试任务中指定 category 所以 我可以单独运行集成 junit 和正常的 junit 测试 http weblogs java net blog johnsmart archive 2010 04
  • 使用 async 函数作为 EventEmitter 监听器有什么问题吗?

    我正在编写一个 Node js v10 应用程序 我想使用await在事件监听器函数中 所以我做了一个async监听器功能 根据下面的代码 它似乎有效 但我很好奇注册时是否有隐藏的缺点或我应该注意的事情async功能作为EvenEmitte
  • 尽管提供了接受属性,Antd 上传程序仍然接受所有文件

    我正在使用 antd 拖放组件https ant design components upload components upload demo drag https ant design components upload compone
  • Flexbox 顺序和选项卡导航

    我想用显示 柔性改变order的 div 与line类 但我想保持这个 TAB 导航顺序 A B C D 正如您在代码片段中看到的 第一个示例工作正常 DOM 序列与 Order 相同 但在第二个示例中 选项卡遵循 DOM 序列 不使用 j
  • 编译一个快速修复程序

    我正在尝试使用 QuickFix 库通过 FIX 协议连接到代理 我刚刚使用他们提供的文档构建了库 并立即使用他们的示例代码 include quickfix FileStore h include quickfix FileLog h i
  • MongoDB 获取聚合查询的executionStats

    我正在寻找一种方法来检索executionStats用于聚合 当使用 find 时 我可以通过使用轻松检索它们explain https docs mongodb com manual reference explain results 输
  • 防止浏览器缓存 AJAX 调用结果

    看起来如果我使用加载动态内容 get 结果缓存在浏览器中 在 QueryString 中添加一些随机字符串似乎可以解决这个问题 我使用new Date toString 但这感觉就像是黑客攻击 还有其他方法可以实现这一目标吗 或者 如果唯一
  • 如何使用Java读取带有部分的配置文件[重复]

    这个问题在这里已经有答案了 给定一个包含以下内容的文件 upper a A b B words 1 one 2 two 如何参考它们的标头访问这些键 值 Java 的 Properties 类仅处理无节文件 使用 ini4j 库 链接教程
  • C 缓冲区溢出 - 为什么有恒定数量的字节会引发段错误? (Mac OS 10.8 64 位,clang)

    我正在试验 C 中的缓冲区溢出 发现一个有趣的怪癖 对于任何给定的数组大小 似乎有一定数量的溢出字节可以在 SIGABRT 崩溃之前写入内存 例如 在下面的代码中 10 字节数组可以溢出到 26 字节 然后在 27 处崩溃 同样 20 字节
  • 按住按钮“重复射击”

    我已经提到了无数关于按住按钮的其他问题 但与 Swift 相关的问题并不多 我有一个使用 touchUpInside 事件连接到按钮的函数 IBAction func singleFire sender AnyObject code 还有另
  • 我的随机化代码无法离线工作

    我是一个 php 菜鸟 我只是根据我在网上找到的其他一些脚本制作了一个小脚本 它从名为 Random 的文件夹中随机选取 3 张图像并显示它们 当我在线运行脚本时它可以工作 但是当我尝试在 xampp 上离线运行它时 我收到此错误 注意 未
  • UITableView 的第一行在顶部栏下被截断

    我有一个UITabBarController有两个UITableViews 全部都是在故事板中创建的 问题是 在第二个表视图中 表的前几行位于顶栏下方 第一个表视图不会发生这种情况 即使我更改视图的顺序 第一个视图将完美工作 第二个视图将呈
  • 派生类构造函数在 python 中如何工作?

    我有以下基类 class NeuralNetworkBase def init self numberOfInputs numberOfHiddenNeurons numberOfOutputs self inputLayer numpy
  • R:使用 spplot 地图中的自定义调色板

    我正在努力使用在多个多边形上引入自定义调色板spplot来自sp包裹 我正在绘制几个字段并希望显示我的评级 其值可以为 0 1 2 4 或 5 我需要为此使用自定义颜色 我尝试的是 spplot Map zcol Rating col re
  • 仅在现有 iOS 应用程序中对某些视图使用 React Native

    是否可以仅对项目中的一个视图使用 React Native 我已经成功为特定的 iOS 应用程序屏幕添加了 React 视图 使用 与现有 iOS 项目集成 文档中的说明 但我不知道如何从该屏幕获取数据并调用其他 objective c 代
  • VB.Net Xml 反序列化为类

    我在尝试将一些 XML 反序列化到我创建的类中时遇到了一些问题 我得到的错误是 There is an error in XML document 1 2 at System Xml Serialization XmlSerializer