【问题标题】:VB.NET Issue creating object structure to serializeVB.NET 问题创建对象结构以进行序列化
【发布时间】:2014-05-12 18:41:53
【问题描述】:

我被 VB.NET 中对象的 XML 序列化困住了。我创建了类、一些测试 XML 文档和代码。使用我现在拥有的代码,我能够将 XML 文档反序列化为对象,然后将该对象序列化回新的 XML 文档。但是,我坚持在 VB.NET 中创建我的对象,然后进行序列化。调试仅以 System.NullReferenceException 停止。我想不通。请帮忙。

类: Part.vb

Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO
Public Class Part
    Public name As String
    Public id As Integer
    Public partNum As String
    Public matlSpec As String
    Public costMatl As Integer
    Public costOut As Integer
    Public parentId As Integer
    Public partType As String
    Public qty As Integer
    Public departments() As dept



    Public Sub New()
    End Sub

    Public Sub New(ByVal name As String, _
                   ByVal id As Integer, _
                   ByVal partNum As String, _
                   ByVal matlSpec As String, _
                   ByVal costMatl As Integer, _
                   ByVal costOut As Integer, _
                   ByVal parentId As Integer, _
                   ByVal partType As String, _
                   ByVal qty As Integer, _
                   ByVal departments() As dept)
        Me.name = name
        Me.id = id
        Me.partNum = partNum
        Me.matlSpec = matlSpec
        Me.costMatl = costMatl
        Me.costOut = costOut
        Me.parentId = parentId
        Me.partType = partType
        Me.qty = qty
        Me.departments = departments

    End Sub
End Class

Public Class dept
    Public num As Integer
    Public hours As Integer

    Public Sub New()
    End Sub

    Public Sub New(ByVal num As Integer, _
                   ByVal hours As Integer)
        Me.num = num
        Me.hours = hours
    End Sub
End Class

<Serializable>
Public Class Unit
    Public unitParts() As Part

    Public Sub New()
    End Sub

    Public Sub New(ByVal unitParts() As Part)
        Me.unitParts = unitParts
    End Sub
End Class

partTest.xml 用于反序列化的测试文档:

<?xml version="1.0" encoding="utf-8"?>
<Unit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <unitParts>
    <Part>
      <name>Steam End Machining</name>
      <id>101</id>
      <partNum>1057662</partNum>
      <matlSpec>244</matlSpec>
      <costMatl>67531</costMatl>
      <costOut>0</costOut>
      <parentId>0</parentId>
      <partType>Casing</partType>
      <qty>1</qty>
      <departments>
        <dept>
          <num>1035</num>
          <hours>60</hours>
        </dept>
      </departments>
    </Part>
    <Part>
      <name>Steam End Rough Machining</name>
      <id>102</id>
      <partNum>1062530</partNum>
      <matlSpec>181</matlSpec>
      <costMatl>2150</costMatl>
      <costOut>56321</costOut>
      <parentId>0</parentId>
      <partType>Casing</partType>
      <qty>6</qty>
      <departments>
        <dept>
          <num>1035</num>
          <hours>60</hours>
        </dept>
        <dept>
          <num>1037</num>
          <hours>25</hours>
        </dept>
        <dept>
          <num>1071</num>
          <hours>7</hours>
        </dept>
      </departments>
    </Part>
    <Part>
      <name>Steam End Casting Top</name>
      <id>103</id>
      <partNum>1060551</partNum>
      <matlSpec>274</matlSpec>
      <costMatl>5434</costMatl>
      <costOut>36635</costOut>
      <parentId>0</parentId>
      <partType>Casing</partType>
      <qty>5</qty>
      <departments>
        <dept>
          <num>1035</num>
          <hours>60</hours>
        </dept>
      </departments>
    </Part>
  </unitParts>
</Unit>

Module_XML(从 Main_Load 调用)

Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO

Module Module_XML
    Public Sub TestPart()
        Dim xmlFile As FileStream = New FileStream("c:\vs2013\ACE\ACE\XML\partTest.xml", FileMode.Open)
        Dim serializer As XmlSerializer = New XmlSerializer(GetType(Unit))
        Dim xUnit As Unit = New Unit

        xUnit = serializer.Deserialize(xmlFile)
        xmlFile.Close()

        Call WriteXMLTest()
    End Sub

TestPart() 成功反序列化上述 XML 文档。 使用它:

Call WriteXML(serializer, xUnit, "c:\vs2013\ACE\ACE\XML\writeTest.xml")

成功将 xUnit 序列化为新文档。


在 WriteXMLTest() 中,我试图构建一个对象结构,然后对其进行序列化。这是我失败的地方:

    Public Sub WriteXMLTest()

        Dim wUnit As Unit = New Unit
        Dim wPart As Part = New Part
        Dim wDept As dept = New dept()

        With wPart
            .name = "Turbine Assembly"
            .id = 106
            .partNum = "1056732"
            .matlSpec = "244"
            .costMatl = 50403
            .costOut = 0
            .parentId = 0
            .partType = "Unit"
            .qty = 1
        End With

        wDept.num = 1056
        wDept.hours = 233
        wPart.departments(0) = wDept
        wUnit.unitParts(0) = wPart

        Dim serializer2 As XmlSerializer = New XmlSerializer(GetType(Unit))
        Call WriteXML(serializer2, wUnit, "c:\vs2013\ACE\ACE\XML\writeTest2.xml")
    End Sub

    Public Sub WriteXML(ByRef serializer As XmlSerializer, ByRef obj As Object, ByVal path As String)
        Dim file As New StreamWriter(path)
        serializer.Serialize(file, obj)
        file.Close()
    End Sub

    Public Function qValidator(ByVal q) As Boolean
        If q IsNot Nothing Then
            Return True
        Else
            Return False
        End If
    End Function
End Module

我认为我的问题在于我的课程或我如何尝试添加到课程中。我已经被这个问题困扰了几天,似乎无法弄清楚。

【问题讨论】:

    标签: xml vb.net class serialization


    【解决方案1】:

    那一行造成了问题

    wPart.departments(0) = wDept 
    

    此时部门仍然为空。

    你知道如何使用调试器,它会向你显示错误。

    要解决这个问题,请将这两行更改为

      wPart.departments = {wDept}
      wUnit.unitParts = {wPart}
    

    【讨论】:

    • Ezi,就是这样!谢谢。至于调试器,我得到了 System.NullReferenceException 但我不知道如何修复它。再次感谢。
    猜你喜欢
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-11
    • 2016-12-01
    • 2010-12-10
    • 1970-01-01
    相关资源
    最近更新 更多