看看我尝试做的是通过XSD为此构建XML我使用的是使用xsd.exe工具构建的类(VOUCHER_BUILD_VERSION_2.vb)基本上有两个子节点MSGDATA和FieldTypes在类中被翻译为两个属性。
第一个节点没有问题,因为该节点的所有子节点都是使用此代码构建的简单XML片段:
Dim Addenda As New VOUCHER_BUILD_TypeShape()
Addenda.FieldTypes = New FieldTypes_TypeShape
With Addenda.FieldTypes
.VCHR_BANK_STG = New FieldTypesVCHR_BANK_STG_TypeShape
With .VCHR_BANK_STG
.class = "R"
.BUSINESS_UNIT = New FieldTypesCharFieldType
...
End With
End With
Dim xs As New XmlSerializer(GetType(VOUCHER_BUILD_TypeShape))
Dim fs As New FileStream("C:/filesPS/demo.xml", FileMode.Create)
xs.Serialize(fs, Addenda)
fs.Close()
什么生成以下XML:
<VOUCHER_BUILD xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<FieldTypes>
<VCHR_VNDR_STG class="R">
<BUSINESS_UNIT type="CHAR"/>
...
</VCHR_VNDR_STG>
</FieldTypes>
</VOUCHER>
问题在于其他节点(MSGDATA)是一种复杂类型,XML中的结果应该是节点:
<MsgData>
<Transaction>
<VCHR_HDR_STG class="R">
<BUSINESS_UNIT>MEX01</BUSINESS_UNIT>
...
</VCHR_HDR_STG>
<PSCAMA class="R">
<LANGUAGE_CD/>
<AUDIT_ACTN>A</AUDIT_ACTN>
...
</PSCAMA>
...
</Transaction>
</MsgData>
正在努力创建:
首先我创建了MSGDATA节点:
Dim oTransaction_TypeShape(0) As Transaction_TypeShape
Addenda.MsgData = oTransaction_TypeShape
在FieldTypes节点至少给我
之后,这是可行的尝试放置内容后:
Dim oTransaction_TypeShape(0) As Transaction_TypeShape
'THIS LINE IS THAT I'M ADDING THAT THEORY IS NODE VCHR_HDR_STG
oTransaction_TypeShape(0).VCHR_HDR_STG = New VCHR_HDR_STGMsgDataRecord_TypeShape
Addenda.MsgData = oTransaction_TypeShape
编译时不标记任何错误。但它在运行时抛出此错误:对象引用未设置为对象的实例。
看起来很简单,但我已经无法解决。
非常感谢你的帮助。
答案 0 :(得分:0)
我终于解决了,错误是一个没有价值的属性(Nothing):
Dim oTransaction_TypeShape(0) As Transaction_TypeShape
oTransaction_TypeShape(0) = New Transaction_TypeShape
oTransaction_TypeShape(0).VCHR_HDR_STG = New VCHR_HDR_STGMsgDataRecord_TypeShape