问题描述
我正在将 VB.net 类序列化为 XML。一个条件是我必须包含 NULL Elements/Nodes 当序列化的 XML 中没有类数据时。为了玩弄这个概念,我修改了 Microsoft 站点上的示例,其中包含采购订单示例。我已将其修改为 在 OrderedItem 类中包含一个子类 (ItemLocation)。也试验了这个类 当它没有类数据时 -- IsNull。
我在 ItemLocation 类定义中使用以下内容
我得到以下 XML 输出
<Stores>
<ItemLocation xsi:nil="true" />
<ItemLocation xsi:nil="true" />
</Stores>
--或--
<Stores>
<StoreLocation/>
<StoreName/>
</Stores>
My Vb.NET Program
<XmlRootAttribute("PurchaSEOrder",Namespace:="http://www.cpandl.com",IsNullable:=False)>
Public Class PurchaSEOrder
Public ShipTo As Address
Public OrderDate As String
' The XmlArrayAttribute changes the XML element name
' from the default of "OrderedItems" to "Items".
<XmlArrayAttribute("Items")>
Public OrderedItems() As OrderedItem
Public SubTotal As Decimal
Public ShipCost As Decimal
Public TotalCost As Decimal
End Class
Public Class Address
' The XmlAttribute instructs the XmlSerializer to serialize the Name
' field as an XML attribute instead of an XML element (the default
' behavior).
<XmlAttribute()>
Public Name As String
Public Line1 As String
' Setting the IsNullable property to false instructs the
' XmlSerializer that the XML attribute will not appear if
' the City field is set to a null reference.
<XmlElementAttribute(IsNullable:=False)>
Public City As String
Public State As String
Public Zip As String
End Class
Public Class OrderedItem
Public ItemName As String
Public Description As String
Public UnitPrice As Decimal
Public Quantity As Integer
Public Linetotal As Decimal
<XmlArrayAttribute("Stores")>
Public ItemLocation() As ItemLocation
' Calculate is a custom method that calculates the price per item,' and stores the value in a field.
Public Sub Calculate()
Linetotal = UnitPrice * Quantity
End Sub
End Class
Public Class ItemLocation
<XmlElement(IsNullable:=True)> Public Group As String
Public StoreName As String
Public StoreCity As String
End Class
Public Shared Sub Main()
' Read and write purchase orders.
Dim t As New test()
t.CreatePO("C:\Users\someuser\Downloads\vb_dump\out.xml")
End Sub
Private Sub CreatePO(filename As String)
' Create an instance of the XmlSerializer class;
' specify the type of object to serialize.
Dim serializer As New XmlSerializer(GetType(PurchaSEOrder))
Dim writer As New StreamWriter(filename)
Dim po As New PurchaSEOrder()
' Create an address to ship and bill to.
Dim billAddress As New Address()
billAddress.Name = "Teresa Atkinson"
billAddress.Line1 = "1 Main St."
billAddress.City = "AnyTown"
billAddress.State = "WA"
billAddress.Zip = "00000"
' Set ShipTo and BillTo to the same addressee.
po.ShipTo = billAddress
po.OrderDate = System.DateTime.Now.ToLongDateString()
' Create an OrderedItem object.
Dim items(1) As OrderedItem
Dim i1 As New OrderedItem()
i1.ItemName = "Widget S"
i1.Description = "Small widget"
i1.UnitPrice = CDec(5.23)
i1.Quantity = 3
i1.ItemLocation = New ItemLocation(1) {} ' Initialize ItemLocation
i1.Calculate()
Dim stores(1) As ItemLocation
Dim s1 As New ItemLocation()
s1.StoreName = "Target"
s1.StoreCity = "MyCity"
stores(0) = s1
i1.ItemLocation(0) = stores(0)
' Insert the item into the array.
items(0) = i1
Dim i2 As New OrderedItem()
i2.ItemName = "Widget x"
i2.Description = "Extra Small widget"
i2.UnitPrice = CDec(500.23)
i2.Quantity = 100
i2.ItemLocation = New ItemLocation(1) {} ' Initialize ItemLocation
i2.Calculate()
Dim s2 As New ItemLocation()
stores(1) = s2
i1.ItemLocation(1) = stores(1)
' Insert the item into the array.
items(1) = i2
po.OrderedItems = items
' Calculate the total cost.
Dim subTotal As New Decimal()
Dim oi As OrderedItem
For Each oi In items
subTotal += oi.Linetotal
Next oi
po.SubTotal = subTotal
po.ShipCost = CDec(12.51)
po.TotalCost = po.SubTotal + po.ShipCost
' Serialize the purchase order,and close the TextWriter.
serializer.Serialize(writer,po)
writer.Close()
End Sub
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)