我需要序列化一个复合对象。我有一个对象“Trailer”,其中包含“Items”的“Shipments”。当我像这样使用 SoapFormatter 序列化对象时:
Trailer trailer = new Trailer() { TrailerID = "1234" };
Shipment shipment1 = new Shipment() { ShipmentNo = "123456789", CarrierCode = "1234", SupplierCode = "12345", CarrierProNo = "1234567", BillOfLadingNo = "11111111", DateNeeded = "2013-04-15", Loc = "000", PackageSlipNo = "11111111", OrderShipped = "2013-02-15", UnloadPriority = "", UpdateID = "" };
shipment1.Items.Add(new Item() { ItemNo = "111111", ItemDescription = "Blah", ItemQty= "11", OrderNo = "1111111", OrderLineNo = "111" });
shipment1.Items.Add(new Item() { ItemNo = "222222", ItemDescription = "Blah 2", ItemQty= "22", OrderNo = "2222", OrderLineNo = "2222" });
trailer.Contents.Add(shipment1);
MemoryStream memoryStream = new MemoryStream();
SoapFormatter formatter = new SoapFormatter();
formatter.Serialize(memoryStream, trailer);
string trailerSoapMessage = Encoding.ASCII.GetString(memoryStream.GetBuffer());
我明白了:
<SOAP-ENV:Body>
<Trailer id="ref-1">
<TrailerID id="ref-2">1234</TrailerID>
<Contents href="#ref-3"/>
</Trailer>
<a1:ArrayList id="ref-3" xmlns:a1="http://schemas.microsoft.com/clr/ns/System.Collections">
<_items href="#ref-4"/>
<_size>2</_size>
<_version>2</_version>
</a1:ArrayList>
<SOAP-ENC:Array id="ref-4" SOAP-ENC:arrayType="xsd:anyType[4]">
<item href="#ref-5"/>
<item href="#ref-6"/>
</SOAP-ENC:Array>
<Shipment id="ref-5">
<ASNNumber id="ref-7">123456789</ASNNumber>
<SupplierCode id="ref-8">12345</SupplierCode>
<Loc id="ref-9">000</Loc>
<CarrierCode id="ref-10">12345</CarrierCode>
<CarrierProNo id="ref-11">12223</CarrierProNo>
<BillOfLadingNo href="#ref-7"/>
<PackageSlipNo href="#ref-7"/>
<UpdateID id="ref-12"></UpdateID>
<UnloadPriority href="#ref-12"/>
<DateNeeded id="ref-13">2013-03-15</DateNeeded>
<OrderShipped id="ref-14">2013-02-15</OrderShipped>
<Items href="#ref-15"/>
</Shipment>
.....
我想要一条更结构化的消息,其中 Trailer 作为包含物品的货件的顶部节点。我还想删除添加到节点的 id 属性,而不是序列化 ArrayList 元数据。我意识到这是设计使然,但我不想要/不需要额外的数据。这可能是使用错误工具完成工作的情况。
谢谢