类型的值无法转换为< 1维数组的... VB

时间:2015-06-24 15:41:34

标签: arrays xml vb.net

EDIT2:找到解决方案,感谢Fabio。

标题有我在尝试填充XML文件时收到的错误消息。

这是一些代码

<!-- language: lang-vb -->

        <System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1"),  _
         System.SerializableAttribute(),  _
         System.Diagnostics.DebuggerStepThroughAttribute(),  _
         System.ComponentModel.DesignerCategoryAttribute("code"),  _
         System.Xml.Serialization.XmlTypeAttribute([Namespace]:="urn:com.x.x"),  _
         System.Xml.Serialization.XmlRootAttribute("MT_AnalyseQueueResponse_EXT", [Namespace]:="urn:com.x.x", IsNullable:=false)>  _
        Partial Public Class DT_AnalyseQueue_EXT

            Private resField As DT_AnalyseQueue_EXTRes
                <System.Xml.Serialization.XmlElementAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)>  _
            Public Property Res() As DT_AnalyseQueue_EXTRes
                Get
                    Return Me.resField
                End Get
                Set
                    Me.resField = value
                End Set
            End Property
        End Class

        <System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1"),  _
         System.SerializableAttribute(),  _
         System.Diagnostics.DebuggerStepThroughAttribute(),  _
         System.ComponentModel.DesignerCategoryAttribute("code"),  _
         System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=true, [Namespace]:="urn:com.x.x")>  _
        Partial Public Class DT_AnalyseQueue_EXTRes
          Private locationField() As DT_AnalyseQueue_EXTResLocation
          '''<remarks/>
          <System.Xml.Serialization.XmlElementAttribute("Location", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
          Public Property Location() As DT_AnalyseQueue_EXTResLocation()
            Get
              Return Me.locationField
            End Get
            Set(value As DT_AnalyseQueue_EXTResLocation())
              Me.locationField = value
            End Set
          End Property
        End Class
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1"), _
 System.SerializableAttribute(), _
 System.Diagnostics.DebuggerStepThroughAttribute(), _
 System.ComponentModel.DesignerCategoryAttribute("code"), _
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:="urn:com.x.x")> _
Partial Public Class DT_AnalyseQueue_EXTRes

  Private locationField() As DT_AnalyseQueue_EXTResLocation

  Private resultsField() As DT_AnalyseQueue_EXTResResultsCylinder

  '<remarks/>
  <System.Xml.Serialization.XmlElementAttribute("Location", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
  Public Property Location() As DT_AnalyseQueue_EXTResLocation()
    Get
      Return Me.locationField
    End Get
    Set(value As DT_AnalyseQueue_EXTResLocation())
      Me.locationField = value
    End Set
  End Property

  '<remarks/>
  <System.Xml.Serialization.XmlArrayAttribute(Form:=System.Xml.Schema.XmlSchemaForm.Unqualified), _
   System.Xml.Serialization.XmlArrayItemAttribute("Cylinder", GetType(DT_AnalyseQueue_EXTResResultsCylinder), Form:=System.Xml.Schema.XmlSchemaForm.Unqualified, IsNullable:=False)> _
  Public Property Results() As DT_AnalyseQueue_EXTResResultsCylinder()
    Get
      Return Me.resultsField
    End Get
    Set(value As DT_AnalyseQueue_EXTResResultsCylinder())
      Me.resultsField = value
    End Set
  End Property
End Class
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1"), _
 System.SerializableAttribute(), _
 System.Diagnostics.DebuggerStepThroughAttribute(), _
 System.ComponentModel.DesignerCategoryAttribute("code"), _
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:="urn:com.xx.xx")> _
Partial Public Class DT_AnalyseQueue_EXTResLocation

  Private noField As String

  Private testpointField As String

  '<remarks/>
  <System.Xml.Serialization.XmlAttributeAttribute()> _
  Public Property No() As String
    Get
      Return Me.noField
    End Get
    Set(value As String)
      Me.noField = value
    End Set
  End Property

  '<remarks/>
  <System.Xml.Serialization.XmlAttributeAttribute()> _
  Public Property Testpoint() As String
    Get
      Return Me.testpointField
    End Get
    Set(value As String)
      Me.testpointField = value
    End Set
  End Property
End Class
<System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "4.0.30319.1"), _
 System.SerializableAttribute(), _
 System.Diagnostics.DebuggerStepThroughAttribute(), _
 System.ComponentModel.DesignerCategoryAttribute("code"), _
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:="urn:com.xxx.xx")> _
Partial Public Class DT_AnalyseQueue_EXTResResultsCylinder

  Private detailField() As DT_AnalyseQueue_EXTResResultsCylinderDetail

  Private dateField As String

  Private batchNoField As String

  Private resultField As String

  Private infoField As String

  Private bIDField As String

  Private completeField As String

  Private releaseField As String

  '<remarks/>
  <System.Xml.Serialization.XmlElementAttribute("Detail", Form:=System.Xml.Schema.XmlSchemaForm.Unqualified)> _
  Public Property Detail() As DT_AnalyseQueue_EXTResResultsCylinderDetail()
    Get
      Return Me.detailField
    End Get
    Set(value As DT_AnalyseQueue_EXTResResultsCylinderDetail())
      Me.detailField = value
    End Set
  End Property

  '<remarks/>
  <System.Xml.Serialization.XmlAttributeAttribute()> _
  Public Property [Date]() As String
    Get
      Return Me.dateField
    End Get
    Set(value As String)
      Me.dateField = value
    End Set
  End Property

  '<remarks/>
  <System.Xml.Serialization.XmlAttributeAttribute()> _
  Public Property BatchNo() As String
    Get
      Return Me.batchNoField
    End Get
    Set(value As String)
      Me.batchNoField = value
    End Set
  End Property

  '<remarks/>
  <System.Xml.Serialization.XmlAttributeAttribute()> _
  Public Property Result() As String
    Get
      Return Me.resultField
    End Get
    Set(value As String)
      Me.resultField = value
    End Set
  End Property

  '<remarks/>
  <System.Xml.Serialization.XmlAttributeAttribute()> _
  Public Property Info() As String
    Get
      Return Me.infoField
    End Get
    Set(value As String)
      Me.infoField = value
    End Set
  End Property

  '<remarks/>
  <System.Xml.Serialization.XmlAttributeAttribute()> _
  Public Property BID() As String
    Get
      Return Me.bIDField
    End Get
    Set(value As String)
      Me.bIDField = value
    End Set
  End Property

  '<remarks/>
  <System.Xml.Serialization.XmlAttributeAttribute()> _
  Public Property Complete() As String
    Get
      Return Me.completeField
    End Get
    Set(value As String)
      Me.completeField = value
    End Set
  End Property

  '<remarks/>
  <System.Xml.Serialization.XmlAttributeAttribute()> _
  Public Property Release() As String
    Get
      Return Me.releaseField
    End Get
    Set(value As String)
      Me.releaseField = value
    End Set
  End Property
End Class

    Public Sub() 
  Dim Mydet As New DT_AnalyseQueue_EXTResResultsCylinderDetail() With {.Component = "", .Limit = ""}
  Dim MyresCy As New DT_AnalyseQueue_EXTResResultsCylinder() With {.BatchNo = "", .Detail = {Mydet}}
  Dim Myloc As New DT_AnalyseQueue_EXTResLocation() With {.No = sLocNo, .Testpoint = sTestP}
  Dim Myres As New DT_AnalyseQueue_EXTRes() With {.Location = {Myloc}, .Results = {MyresCy}}
  Dim MyCylE As New DT_AnalyseQueue_EXT() With {.Res = Myres}

  Dim MyXMLSerializerLoc As New XmlSerializer(GetType(DT_AnalyseQueue_EXT))
      MyXMLSerializerLoc.Serialize(MyXMLWriter, MyCylE)
    End Sub

sub中带有*标记的代码告诉显示错误消息的位置。 在我看来,定义可能是错误的,但实际上我认为DT_AnalyseQueue_EXTResLocation被定义为一个数组。 + 编辑:

所以我以Fabio建议的方式添加了信息和代码。 现在错误信息是 类型的值&#39; CustomProjectGlobal.DT_AnalyseQueue_EXTResResultsCylinder&#39;无法转换为CustomProjectGlobal的一维数组的一维数组.DT_AnalyseQueue_EXTResResultsCylinder&#39;

如果我没有实现该部分,序列化程序仍然会生成错误而没有XML文件。

2 个答案:

答案 0 :(得分:1)

至少这段代码会抛出这种异常

.Location = New DT_AnalyseQueue_EXTResLocation() With {.No = sLocNo, .Testpoint = sTestP}

属性Location是类型DT_AnalyseQueue_EXTRes

数组
Private locationField() As DT_AnalyseQueue_EXTResLocation

Public Property Location() As DT_AnalyseQueue_EXTResLocation()
Get
    Return Me.locationField
End Get
Set(value As DT_AnalyseQueue_EXTResLocation())
    Me.locationField = value
End Set
End Property

来自MSDN Arrays in Visual Basic

  

您声明一个数组变量的方式与任何其他变量相同   使用Dim语句。 您可以使用类型或变量名称   一对或多对括号表示它将持有一个   数组而不是标量,它是包含单个变量的变量   值。

但您尝试分配DT_AnalyseQueue_EXTResLocation

类型的变量

尝试将您的行更改为下一行:

Dim loc As New DT_AnalyseQueue_EXTResLocation() With {.No = sLocNo, .Testpoint = sTestP }
Dim res As New DT_AnalyseQueue_EXTRes() With {.Location = {loc}}
Dim MyCylE As New DT_AnalyseQueue_EXT() With {.Res = res }

代码.Location = {loc}将创建一个包含一个元素的数组,并将其分配给.Location属性 对不起,根据我的意见,有时候一行代码可能无法读取

答案 1 :(得分:0)

你应该坚持今天早上的工作。我试图解决很多新的错误

Imports System.Xml
Imports System.Xml.Serialization
Module Module1

    Sub Main()

    End Sub

End Module
<XmlRoot("MT_AnalyseQueueResponse_EXT")> _
 Partial Public Class DT_AnalyseQueue_EXT

    Private resField As DT_AnalyseQueue_EXTResLocation
    <XmlElement("DT_AnalyseQueue_EXTRes")> _
    Public Property Res() As DT_AnalyseQueue_EXTResLocation
        Get
            Return Me.resField
        End Get
        Set(ByVal value As DT_AnalyseQueue_EXTResLocation)
            Me.resField = value
        End Set
    End Property
End Class

<XmlRoot("DT_AnalyseQueue_EXTResLocation")> _
Partial Public Class DT_AnalyseQueue_EXTResLocation
    Private locationField() As DT_AnalyseQueue_EXTResLocation
    '''<remarks/>
    <XmlElement("Location")> _
    Public Property Location() As DT_AnalyseQueue_EXTResLocation()
        Get
            Return Me.locationField
        End Get
        Set(ByVal value As DT_AnalyseQueue_EXTResLocation())
            Me.locationField = value
        End Set
    End Property

    Private noField As String

    Private testpointField As String

    '<remarks/>
    <XmlAttributeAttribute("No")> _
    Public Property No() As String
        Get
            Return Me.noField
        End Get
        Set(ByVal value As String)
            Me.noField = value
        End Set
    End Property
End Class
<XmlRoot("DT_AnalyseQueue_EXTResultsCyclinderDetail")> _
Partial Public Class DT_AnalyseQueue_EXTResultsCyclinderDetail

    Private locationField() As DT_AnalyseQueue_EXTResLocation
    Private resultsField() As DT_AnalyseQueue_EXTResResultsCylinder

    '<remarks/>
    <XmlElement("Location")> _
    Public Property Location() As DT_AnalyseQueue_EXTResLocation()
        Get
            Return Me.locationField
        End Get
        Set(ByVal value As DT_AnalyseQueue_EXTResLocation())
            Me.locationField = value
        End Set
    End Property

    '<remarks/>
    <XmlElement("Cylinder")> _
    Public Property Results() As DT_AnalyseQueue_EXTResResultsCylinder()
        Get
            Return Me.resultsField
        End Get
        Set(ByVal value As DT_AnalyseQueue_EXTResResultsCylinder())
            Me.resultsField = value
        End Set
    End Property
End Class

<XmlRoot("DT_AnalyseQueue_EXTResResultsCylinder")> _
Partial Public Class DT_AnalyseQueue_EXTResResultsCylinder

    Private detailField() As DT_AnalyseQueue_EXTResultsCyclinderDetail

    Private dateField As String

    Private batchNoField As String

    Private resultField As String

    Private infoField As String

    Private bIDField As String

    Private completeField As String

    Private releaseField As String

    '<remarks/>
    <XmlElement("Detail")> _
    Public Property Detail() As DT_AnalyseQueue_EXTResultsCyclinderDetail()
        Get
            Return Me.detailField
        End Get
        Set(ByVal value As DT_AnalyseQueue_EXTResultsCyclinderDetail())
            Me.detailField = value
        End Set
    End Property

    '<remarks/>
    <XmlAttribute("Date")> _
    Public Property [Date]() As String
        Get
            Return Me.dateField
        End Get
        Set(ByVal value As String)
            Me.dateField = value
        End Set
    End Property

    '<remarks/>
    <XmlAttributeAttribute("BatchNo")> _
    Public Property BatchNo() As String
        Get
            Return Me.batchNoField
        End Get
        Set(ByVal value As String)
            Me.batchNoField = value
        End Set
    End Property

    '<remarks/>
    <XmlAttribute("Result")> _
    Public Property Result() As String
        Get
            Return Me.resultField
        End Get
        Set(ByVal value As String)
            Me.resultField = value
        End Set
    End Property

    '<remarks/>
    <XmlAttribute("Info")> _
    Public Property Info() As String
        Get
            Return Me.infoField
        End Get
        Set(ByVal value As String)
            Me.infoField = value
        End Set
    End Property

    '<remarks/>
    <XmlAttribute("BID")> _
    Public Property BID() As String
        Get
            Return Me.bIDField
        End Get
        Set(ByVal value As String)
            Me.bIDField = value
        End Set
    End Property

    '<remarks/>
    <XmlAttributeAttribute("Complete")> _
    Public Property Complete() As String
        Get
            Return Me.completeField
        End Get
        Set(ByVal value As String)
            Me.completeField = value
        End Set
    End Property

    '<remarks/>
    <XmlAttribute("Release")> _
    Public Property Release() As String
        Get
            Return Me.releaseField
        End Get
        Set(ByVal value As String)
            Me.releaseField = value
        End Set
    End Property
End Class
相关问题