簡體   English   中英

VB.NET問題創建對象結構以進行序列化

[英]VB.NET Issue creating object structure to serialize

我被困在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

我認為我的問題出在我的課堂上,還是我試圖添加到他們身上的方式。 我已經堅持了幾天,似乎無法弄清楚。

那條線造成了問題

wPart.departments(0) = wDept 

此時,部門仍然為空。

您知道如何使用調試器嗎,它將向您顯示該錯誤。

要解決該問題,請將這兩行更改為

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM