質問

I am stuck on XML serialization of objects in VB.NET. I have created classes, some test XML documents and code. With the code I have now, I am able to deserialize an XML document to objects and then serialize that object back to a new XML document. However, I am stuck on creating my objects within VB.NET and then serializing. The debug just stops with a System.NullReferenceException. I can't figure it out. Please help.

Classes: 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 Test Document used to deserialize:

<?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 (Called from 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() successfully deserializes the XML document above. Using that with:

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

successfully serializes xUnit to a new document.


In WriteXMLTest(), I am trying to buildup an object structure and then serialize it. This is where I fail:

    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

I think my problem lies in either my classes or how I'm attempting to add to them. I've been stuck on this for a couple days and can't seem to figure it out.

役に立ちましたか?

解決

That line makes the problem

wPart.departments(0) = wDept 

The departments is still null at this point.

Do you know how to use a debugger it would show it to you that error.

To fix that problem, change that two lines to

  wPart.departments = {wDept}
  wUnit.unitParts = {wPart}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top