VB.NET问题创建要序列化的对象结构

时间:2014-05-12 18:41:53

标签: xml vb.net class serialization

我坚持使用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

我认为我的问题在于我的课程或我是如何尝试添加的。我已经被困在这几天了,似乎无法解决这个问题。

1 个答案:

答案 0 :(得分:0)

该行会产生问题

wPart.departments(0) = wDept 

此时部门仍为空。

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

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

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