将 Web 服务 API 中的 XML 字符串反序列化为 C# 对象

Posted

技术标签:

【中文标题】将 Web 服务 API 中的 XML 字符串反序列化为 C# 对象【英文标题】:Deserializing XML String from Web Service API into C# Object 【发布时间】:2020-04-17 00:37:39 【问题描述】:

我是一名中级新手程序员,我试图为在没有通知的情况下离开我们团队的人接手一个项目。我真的希望这里的某个人可以在这里阐明我做错了什么的方式。我相信它与 XML 结构及其定义方式有关。

问题重述: 我正在尝试从我们的物流系统数据库中读取 XML 并将其转换为 C# 对象。我通过 Web 服务 API 进入数据库,该 API 将相关 XML 文件作为字符串返回。然而,当我将 XML 字符串反序列化为一个对象时,我得到的结果是空白/空元素。

XSD 文件: XML 文件本身非常复杂,因此这里是该文件的 XSD 链接:https://www.magaya.com/XMLSchemas/V1/Shipment.xsd。

XML 字符串位于代码块下方。确保一直向右滚动才能正确复制非常大。

这是我用来尝试将其反序列化为对象的 C# 代码。请原谅马虎,解决这个问题一直很艰难。

反序列化的C#代码:

    public static ShipmentType GetShipmentObject(string shipmentXML)
         // Method to deserialize shipment XML into a C# object...
             ShipmentType ShipmentObject = null;
           try
            
                if (!string.IsNullOrWhiteSpace(shipmentXML))
                
                    using (TextReader reader = new StringReader(shipmentXML))
                    
                        XmlRootAttribute xr = new XmlRootAttribute();
                        xr.Namespace = "http://www.magaya.com/XMLSchema/V1";
                        xr.ElementName = "Shipments";
                        xr.DataType = "OceanShipment";
                        //xr.IsNullable = false;
                        XmlSerializer deserializer = new XmlSerializer(typeof(ShipmentType),xr);
                        ShipmentObject = (ShipmentType)deserializer.Deserialize(reader);
                        return ShipmentObject;
                    
                
            
            catch (Exception ex)
            
                MessageBox.Show(ex.Message);
                //LOGManager.LogFatal(ex, ex.Message);
            
            return ShipmentObject;   
        

下面是这个类返回的 XML 字符串。它直接来自 Visual Studio 的调试器。不会反序列化的是这个字符串。我替换了一些值以保护隐私,但不存在其他 xml 结构更改。我为长度道歉,下面的所有内容都只是 xml 字符串,所以从技术上讲,这就是我的帖子的结尾--->>

<?xml version="1.0" encoding="utf-8"?>
<Shipments xmlns="http://www.magaya.com/XMLSchema/V1">
  <OceanShipment GUID="99" Type="SH">
    <CreatedOn>2019-12-26T16:29:59-05:00</CreatedOn>
    <Number>EWL24195</Number>
    <CreatedByName>Madison Fernandez</CreatedByName>
    <Version>106</Version>
    <ModeOfTransportation Code="11">
      <Description>FCL-DOOR</Description>
      <Method>Ocean</Method>
    </ModeOfTransportation>
    <ModeOfTransportCode>11</ModeOfTransportCode>
    <IssuedByName>KS Co. LTD</IssuedByName>
    <IssuedBy GUID="35b740a3-7e4b-45e8-9e69-d3c9ea9d4fb8">
      <Type>ForwardingAgent</Type>
      <Name>KS Co. LTD</Name>
      <CreatedOn>2019-08-28T15:35:19-05:00</CreatedOn>
      <Address>
        <Street>99</Street>
        <City>city test</City>
        <State>state test</State>
        <ZipCode>99</ZipCode>
        <Country Code="CN">CN</Country>
        <Description>Default Shipping Address</Description>
      </Address>
      <BillingAddress>
        <Street>99</Street>
        <City>city test</City>
        <State>state test</State>
        <ZipCode>99</ZipCode>
        <Country Code="CN">CN</Country>
        <Description>Default Shipping Address</Description>
      </BillingAddress>
      <TransactionDueDays>10</TransactionDueDays>
      <PaymentTerms>
        <Description>Net 10</Description>
        <NetDueDays>10</NetDueDays>
        <DiscountPercentage>0.00</DiscountPercentage>
        <DiscountPaidDays>0</DiscountPaidDays>
      </PaymentTerms>
      <IsPrepaid>false</IsPrepaid>
    </IssuedBy>
    <IssuedByAddress>
      <Street>99</Street>
      <City>test</City>
      <State>test</State>
      <ZipCode>test</ZipCode>
      <Country Code="CN">CN</Country>
      <Description>Default Shipping Address</Description>
    </IssuedByAddress>
    <ShipperName>Standard Fiber</ShipperName>
    <Shipper GUID="999999">
      <Type>Vendor</Type>
      <Name>dummy shipper</Name>
      <CreatedOn>2012-07-02T18:36:53-05:00</CreatedOn>
      <Address>
        <Street>test</Street>
        <Street>test</Street>
        <City>Shanghai</City>
        <Country Code="CN">CN</Country>
        <Description>Default Shipping Address</Description>
      </Address>
      <BillingAddress>
        <Street>test</Street>
        <Street>test</Street>
        <City>Shanghai</City>
        <Country Code="CN">CN</Country>
        <Description>Default Billing Address</Description>
      </BillingAddress>
      <Phone>985818</Phone>
      <AccountNumber>13283</AccountNumber>
      <IsPrepaid>true</IsPrepaid>
    </Shipper>
    <ShipperAddress>
      <Street>test</Street>
      <Street>test</Street>
      <City>Shanghai</City>
      <Country Code="CN">CN</Country>
      <Description>Default Shipping Address</Description>
    </ShipperAddress>
    <ConsigneeName>Cnee</ConsigneeName>
    <Consignee GUID="186510fc-e1c4-4b8b-a0c4-99363b15bd41">
      <Type>Client</Type>
      <Name>Cnee</Name>
      <CreatedOn>2018-05-08T13:14:48-05:00</CreatedOn>
      <Address>
        <Street>TEST STREET NUMBER</Street>
        <City>Red Bank</City>
        <State>NJ</State>
        <ZipCode>07701</ZipCode>
        <Country Code="US">USA</Country>
        <Description>Default Shipping Address</Description>
      </Address>
      <BillingAddress>
        <Street>TEST STREET NUMBER</Street>
        <City>Red Bank</City>
        <State>NJ</State>
        <ZipCode>07701</ZipCode>
        <Country Code="US">USA</Country>
        <Description>Default Billing Address</Description>
      </BillingAddress>
      <AccountNumber>CX10136</AccountNumber>
      <Balance Currency="USD">114517.15998894054443</Balance>
      <TransactionDueDays>15</TransactionDueDays>
      <PaymentTerms>
        <Description>Net 15</Description>
        <NetDueDays>15</NetDueDays>
        <DiscountPercentage>0.00</DiscountPercentage>
        <DiscountPaidDays>0</DiscountPaidDays>
      </PaymentTerms>
      <IsPrepaid>false</IsPrepaid>
    </Consignee>
    <ConsigneeAddress>
      <Street>TEST STREET NUMBER</Street>
      <City>Red Bank</City>
      <State>NJ</State>
      <ZipCode>07701</ZipCode>
      <Country Code="US">USA</Country>
      <Description>Default Shipping Address</Description>
    </ConsigneeAddress>
    <DestinationAgentName>Empire Worldwide Logistics, LLC</DestinationAgentName>
    <DestinationAgent GUID="978d4f95-8183-4820-830e-a40dfc4cb3d3">
      <Type>ForwardingAgent</Type>
      <Name>Empire Worldwide Logistics, LLC</Name>
      <CreatedOn>2010-08-06T16:27:13-05:00</CreatedOn>
      <Address>
        <Street>360 North Sepulveda Blvd</Street>
        <Street>Suite 1054</Street>
        <City>EL Segundo</City>
        <State>CA</State>
        <ZipCode>90245</ZipCode>
        <Country Code="US">USA</Country>
        <Description>Default Shipping Address</Description>
        <ContactName>Kenneth Quartarolo</ContactName>
        <ContactPhone>424-239-6466</ContactPhone>
        <ContactEmail>dummy@empirewwl.com</ContactEmail>
      </Address>
      <BillingAddress>
        <Street>360 North Sepulveda Blvd</Street>
        <Street>Suite 1054</Street>
        <City>EL Segundo</City>
        <State>CA</State>
        <ZipCode>90245</ZipCode>
        <Country Code="US">USA</Country>
        <Description>Default Billing Address</Description>
        <ContactName>dummy contact</ContactName>
        <ContactPhone>424-239-6466</ContactPhone>
        <ContactEmail>dummy@empirewwl.com</ContactEmail>
      </BillingAddress>
      <Email>dummy@empirewwl.com</Email>
      <Website>empirewwl.com</Website>
      <Phone>424-239-6466</Phone>
      <ContactFirstName>Kenneth</ContactFirstName>
      <ContactLastName>Quartarolo</ContactLastName>
      <ExporterID>123</ExporterID>
      <ExporterIDType>EIN</ExporterIDType>
      <NetworkID>21849</NetworkID>
      <IsKnownShipper>true</IsKnownShipper>
      <IsPrepaid>true</IsPrepaid>
      <AgentInfo>
        <FMCNumber>022706N</FMCNumber>
        <SCACNumber>EWLL</SCACNumber>
      </AgentInfo>
    </DestinationAgent>
    <DestinationAgentAddress>
      <Street>360 North Sepulveda Blvd</Street>
      <Street>Suite 1054</Street>
      <City>EL Segundo</City>
      <State>CA</State>
      <ZipCode>90245</ZipCode>
      <Country Code="US">USA</Country>
      <Description>Default Shipping Address</Description>
      <ContactName>dummy contact</ContactName>
      <ContactPhone>424-239-6466</ContactPhone>
      <ContactEmail>dummy@empirewwl.com</ContactEmail>
    </DestinationAgentAddress>
    <CarrierName>Yang Ming (America) Corporation</CarrierName>
    <Carrier GUID="504c2949-a090-42fd-8161-22dc470593c4">
      <Type>Carrier</Type>
      <Name>Yang Ming (America) Corporation</Name>
      <CreatedOn>2010-09-13T07:44:28-05:00</CreatedOn>
      <Address>
        <Street>181 W.Huntington Drive, Suite 202</Street>
        <City>Monrovia</City>
        <State>CA</State>
        <ZipCode>91016</ZipCode>
        <Country Code="US">USA</Country>
        <Description>Default Billing Address</Description>
        <ContactPhone>626.782.9797</ContactPhone>
        <ContactFax>626.782.9819</ContactFax>
      </Address>
      <BillingAddress>
        <Street>181 W.Huntington Drive, Suite 202</Street>
        <City>Monrovia</City>
        <State>CA</State>
        <ZipCode>91016</ZipCode>
        <Country Code="US">USA</Country>
        <Description>Default Billing Address</Description>
        <ContactPhone>626.782.9797</ContactPhone>
        <ContactFax>626.782.9819</ContactFax>
      </BillingAddress>
      <Phone>626.782.9797 </Phone>
      <Fax>626.782.9819</Fax>
      <Balance Currency="USD">12292.499272404239</Balance>
      <TransactionDueDays>30</TransactionDueDays>
      <PaymentTerms>
        <Description>Net 30</Description>
        <NetDueDays>30</NetDueDays>
        <DiscountPercentage>0.00</DiscountPercentage>
        <DiscountPaidDays>0</DiscountPaidDays>
      </PaymentTerms>
      <OtherAddresses>
        <Address>
          <Street>SSA Terminal Berth 58 - Z985</Street>
          <Street>199 Middle Harbor Rd</Street>
          <Description>SSA Terminal OAK - Z985</Description>
        </Address>
        <Address>
          <Street>WANDO WELCH TERMINAL N598</Street>
          <Street>MT. PLEASANT, SC</Street>
          <Description>WANDO WELCH TERMINAL N598</Description>
        </Address>
        <Address>
          <Street>TraPac Oakland Firms:Y549</Street>
          <Street>2800 7th St. Berth 30,</Street>
          <City>Oakland</City>
          <State>CA</State>
          <ZipCode>94607</ZipCode>
          <Country Code="US">USA</Country>
          <Description>TraPac Oakland Firms:Y549</Description>
        </Address>
        <Address>
          <Street>GCT DELTAPORT(3891)</Street>
          <Street>Delta, BC V4M 4G5</Street>
          <Description>GCT DELTAPORT(3891)</Description>
        </Address>
        <Address>
          <Street>CN - MEMPHIS FIRMS:P484</Street>
          <Street>3588 Paul R. Lowry Road</Street>
          <Description>CN - MEMPHIS FIRMS:P484</Description>
        </Address>
        <Address>
          <Street>UNION PACIFIC RAILROAD K078</Street>
          <Description>UNION PACIFIC RAILROAD K078</Description>
        </Address>
        <Address>
          <Street>BNSF R/R - LOGISTICS PARK FIRMS:J177</Street>
          <Description>FIRMS:J177 KANSAS,KS</Description>
        </Address>
        <Address>
          <Street>(495)CN-BRAMPTON(3037)</Street>
          <Street>30 INTERMODAL DRIVE</Street>
          <Description>(495)CN-BRAMPTON(3037)</Description>
        </Address>
        <Address>
          <Street>NS Rail Landers (CHICAGO) - FIRMS I104</Street>
          <Description>NS Rail Landers (CHICAGO) - FIRMS I104</Description>
        </Address>
        <Address>
          <Street>CN - Indianapolis - FIRMS: H913</Street>
          <Description>CN - Indianapolis - FIRMS: H913</Description>
        </Address>
        <Address>
          <Street>FIRMS:H572 LOGISTICS PARK,IL</Street>
          <Description>BNSF R/R (CHICAGO) - FIRMS H572</Description>
        </Address>
        <Address>
          <Street>(495)CN-BRAMPTON(3037)</Street>
          <Street>30 INTERMODAL DRIVE</Street>
          <Street>BRAMPTON, ON L6T 5K1</Street>
          <Description>(495)CN-BRAMPTON(3037)</Description>
        </Address>
        <Address>
          <Street>CN R/R - CHICAGO/HAREY (FIRMS I092)</Street>
          <Street>15840 WEST AVE. HARVEY, IL</Street>
          <Description>CN R/R - CHICAGO/HAREY (FIRMS I092)</Description>
        </Address>
        <Address>
          <Street>FIRMS:H572 - LOGISTICS PARK,IL</Street>
          <Description>FIRMS:H572 - LOGISTICS PARK,IL</Description>
        </Address>
        <Address>
          <Street>GCT Bayonne FIRM: E364</Street>
          <Street>302 PORT JERSEY BLVD</Street>
          <Street>JERSEY CITY, NJ 07305</Street>
          <Description>GCT Bayonne FIRM: E364</Description>
        </Address>
        <Address>
          <Street>Trapac LAX Terminal - FIRMS Y258</Street>
          <Description>Trapac LAX Terminal - FIRMS Y258</Description>
        </Address>
        <Address>
          <Street>Yusen Terminals FIRMS:Y790</Street>
          <Street>701 New Dock St.</Street>
          <Street>Terminal Island, CA 90731</Street>
          <Description>Yusen Terminals FIRMS:Y790</Description>
        </Address>
        <Address>
          <Street>CSX @ INDIANAPOLIS, IN - FIRMS I950</Street>
          <Description>CSX @ INDIANAPOLIS, IN - FIRMS I950</Description>
        </Address>
        <Address>
          <Street>NS RAIL (ST LOUIS) - FIRMS J288</Street>
          <Description>NS RAIL (ST LOUIS) - FIRMS J288</Description>
        </Address>
        <Address>
          <Street>NS-RICHENBACKER(COLUMBUS)</Street>
          <Street>FIRMS - H367</Street>
          <Description>NS-RICHENBACKER(COLUMBUS)</Description>
        </Address>
        <Address>
          <Street>MASSPORT CONLEY TERMINAL - FIRMS A295</Street>
          <Street>EAST FIRST &amp; FARRAGUT ROAD,</Street>
          <Street>S. BOSTON, MA 02127</Street>
          <Description>MASSPORT CONLEY TERMINAL - FIRMS A295</Description>
        </Address>
        <Address>
          <Street>FIRMS:K961 SALT LAKE CITY,UT</Street>
          <Description>UNION PACIFIC R/R - Salt Lake UT</Description>
        </Address>
        <Address>
          <Street>PIER T136, LONG BEACH, CA</Street>
          <Street>301 HANJIN RD</Street>
          <Description>TTI LGB TRML - FIRMS:Z952</Description>
        </Address>
        <Address>
          <Street>FIRMS:J300 ST. LOUIS,MO</Street>
          <Description>FIRMS:J300 ST. LOUIS,MO</Description>
        </Address>
        <Address>
          <Street>Norfolk APM - FIRMS: N195</Street>
          <Description>Norfolk APM - FIRMS: N195</Description>
        </Address>
        <Address>
          <Street>NCSPA Wilmington - FIRMS L194</Street>
          <Street>#1 SHIPYARD BLVD.</Street>
          <City>Wilmington</City>
          <State>NC</State>
          <ZipCode>28401</ZipCode>
          <Country Code="US">USA</Country>
          <Description>NCSPA Wilmington - FIRMS L194</Description>
        </Address>
        <Address>
          <Street>VANTERM TERMINAL(3395)</Street>
          <Street>1300 STEWART STREET</Street>
          <Street>VANCOUVER, BC V5L 4X5</Street>
          <Description>Vancouver</Description>
        </Address>
        <Address>
          <Street>Firms Code : L738</Street>
          <Description>Garden City Terminal - Firms Code : L738</Description>
        </Address>
        <Address>
          <Street>ITS - FIRMS CODE: Y309</Street>
          <Description>ITS - Berth 234 LGB</Description>
        </Address>
        <Address>
          <Street>APM Terminal - FIRMS W185</Street>
          <Street>2500 Navy Way, Pier 400</Street>
          <Street>Terminal Island, CA 90731</Street>
          <Description>APM Terminals - FIRMS W185</Description>
        </Address>
        <Address>
          <Street>Firms:Z693</Street>
          <Description>Firms:Z693</Description>
        </Address>
        <Address>
          <Street>MARION UP R/R FIRMS: P279</Street>
          <Description>MARION UP R/R FIRMS: P279</Description>
        </Address>
        <Address>
          <Street>FIRMS P279</Street>
        </Address>
        <Address>
          <Street>12330 Lakeland Rd. Santa Fe Springs, CA 90670</Street>
          <City>Santa Fe Springs</City>
          <State>CA</State>
          <ZipCode>90670</ZipCode>
          <Country Code="US">USA</Country>
          <Description>Phoenix Warehouse of CA, LLC.</Description>
        </Address>
        <Address>
          <Street>NORTH CONTAINER TERMINAL - FIRMS: L391</Street>
          <Street>100 Remount Rd. N.</Street>
          <Street>Charleston, SC 29406</Street>
          <Description>NORTH CONTAINER TERMINAL - Charleston</Description>
        </Address>
        <Address>
          <Street>Berth 246 Long Beach CA   FIRMS W182</Street>
          <Street>1521 Harbor Scenic Dr.</Street>
          <Description>LGB TRML FIRMS W182</Description>
          <ContactPhone>562-590-8728</ContactPhone>
        </Address>
        <Address>
          <Street>13131 Daairy Ashford Rd. Suite 300</Street>
          <City>Sugarland</City>
          <State>CA</State>
          <ZipCode>77478</ZipCode>
          <Country Code="US">USA</Country>
          <Description>Yang Ming (America) Corporation</Description>
          <ContactPhone>281.201.5150</ContactPhone>
          <ContactEmail>perdiem@us.yangming.com</ContactEmail>
        </Address>
        <Address>
          <Street>MAHER TERMINAL - E416 - BERTH 64</Street>
          <Street>PORT ELIZABETH, NJ</Street>
          <Description>MAHER TERMINAL - E416 - BERTH 64</Description>
        </Address>
        <Address>
          <Street>Berth 126. San Pedro - FIRMS Y773</Street>
          <City>San Pedro</City>
          <State>CA</State>
          <Country Code="US">USA</Country>
          <Description>WBCT LAX TRML FIRMS Y773</Description>
          <ContactPhone>310.519.2349</ContactPhone>
        </Address>
        <Address>
          <Street>13131 DAIRY ASHFORD RD, SUITE 300</Street>
          <City>Sugarland</City>
          <State>TX</State>
          <ZipCode>77478</ZipCode>
          <Country Code="US">USA</Country>
          <Description>Yang Ming (America) Corporation</Description>
          <ContactPhone>281.201.5150</ContactPhone>
          <ContactEmail>perdiem@us.yangming.com</ContactEmail>
        </Address>
      </OtherAddresses>
      <IsPrepaid>false</IsPrepaid>
      <CarrierInfo>
        <CarrierTypeCode>Ocean</CarrierTypeCode>
        <SCACNumber>YMLU</SCACNumber>
      </CarrierInfo>
    </Carrier>
    <CarrierAddress>
      <Street>181 W.Huntington Drive, Suite 202</Street>
      <City>Monrovia</City>
      <State>CA</State>
      <ZipCode>91016</ZipCode>
      <Country Code="US">USA</Country>
      <Description>Default Billing Address</Description>
      <ContactPhone>626.782.9797</ContactPhone>
      <ContactFax>626.782.9819</ContactFax>
    </CarrierAddress>
    <Items>
      <Item GUID="e500f82c-91e0-4a75-bab1-64ffe10cca3b" Type="WI">
        <Version>105</Version>
        <Status>Loaded</Status>
        <Pieces>1</Pieces>
        <PieceQuantity>0.00</PieceQuantity>
        <IsSummarized>false</IsSummarized>
        <PackageName>40 High Cube</PackageName>
        <WHRItemID>0</WHRItemID>
        <Length Unit="m">12.06000000000000049738</Length>
        <Height Unit="m">2.59100000000000019185</Height>
        <Width Unit="m">2.43800000000000016698</Width>
        <Weight Unit="kg">0.00</Weight>
        <ContainedPiecesWeightIncluded>false</ContainedPiecesWeightIncluded>
        <Volume Unit="m3">76.181299313332</Volume>
        <VolumeWeight Unit="vlb">28005.22985510987928137183</VolumeWeight>
        <PieceWeight Unit="kg">0.00</PieceWeight>
        <PieceVolume Unit="m3">0.00</PieceVolume>
        <Package>
          <Type>Container</Type>
          <Code>CNT</Code>
          <Name>40 High Cube</Name>
          <Length Unit="m">12.06000000000000049738</Length>
          <Height Unit="m">2.59100000000000019185</Height>
          <Width Unit="m">2.43800000000000016698</Width>
          <Volume Unit="m3">76.40000000000000568434</Volume>
          <MaxWeight Unit="kg">26330.00</MaxWeight>
          <ContainerCode>40</ContainerCode>
          <ContainerEquipType>45G0</ContainerEquipType>
          <Methods>
            <Method>Ocean</Method>
            <Method>Ground</Method>
          </Methods>
        </Package>
        <InShipmentGUID>aad8057c-3d3c-4cf3-b0c5-e5e631276dc0</InShipmentGUID>
        <IncludeInSED>false</IncludeInSED>
        <IsContainer>true</IsContainer>
        <InMasterWayBillNumber>MBL4978OI</InMasterWayBillNumber>
        <InHouseWayBillNumber>SH19120796</InHouseWayBillNumber>
        <ArrivalDate>2020-01-28T16:27:02-05:00</ArrivalDate>
        <ContainerInfo>
          <GeneratorSetup>false</GeneratorSetup>
          <IsNonOperatingReefer>false</IsNonOperatingReefer>
        </ContainerInfo>
        <OrderIndex>1</OrderIndex>
        <IsPallet>false</IsPallet>
        <IsOverstock>false</IsOverstock>
        <NotLoaded>false</NotLoaded>
        <InTask>false</InTask>
      </Item>
    </Items>
    <MeasurementUnits>
      <LengthUnit>m</LengthUnit>
      <VolumeUnit>m3</VolumeUnit>
      <WeightUnit>kg</WeightUnit>
      <VolumeWeightUnit>vlb</VolumeWeightUnit>
      <AreaUnit>m2</AreaUnit>
      <LengthPrecision>3</LengthPrecision>
      <VolumePrecision>4</VolumePrecision>
      <WeightPrecision>3</WeightPrecision>
      <VolumeWeightPrecision>3</VolumeWeightPrecision>
      <AreaPrecision>3</AreaPrecision>
      <VolumeWeightFactor>166.00</VolumeWeightFactor>
    </MeasurementUnits>
    <CreatorNetworkID>21849</CreatorNetworkID>
    <Events>
      <Event>
        <Date>2019-12-26T16:29:59-05:00</Date>
        <EventDefinition>
          <Name>1- Booking Pending</Name>
          <IncludeInTracking>true</IncludeInTracking>
        </EventDefinition>
        <IncludeInTracking>true</IncludeInTracking>
        <OwnerType>SH</OwnerType>
        <OwnerNumber>MBL4978OI</OwnerNumber>
        <OwnerGUID>db1fd1e5-a096-4673-a76c-3e629da72bc9</OwnerGUID>
      </Event>
      <Event>
        <Date>2019-12-26T16:29:59-05:00</Date>
        <EventDefinition>
          <Name>1- Booking Pending</Name>
          <IncludeInTracking>true</IncludeInTracking>
        </EventDefinition>
        <IncludeInTracking>true</IncludeInTracking>
        <OwnerType>SH</OwnerType>
        <OwnerNumber>SH19120796</OwnerNumber>
        <OwnerGUID>aad8057c-3d3c-4cf3-b0c5-e5e631276dc0</OwnerGUID>
      </Event>
      <Event>
        <Date>2019-12-26T16:30:20-05:00</Date>
        <EventDefinition>
          <Name>2- Booking Confirmed</Name>
          <IncludeInTracking>true</IncludeInTracking>
        </EventDefinition>
        <IncludeInTracking>true</IncludeInTracking>
        <OwnerType>SH</OwnerType>
        <OwnerNumber>SH19120796</OwnerNumber>
        <OwnerGUID>aad8057c-3d3c-4cf3-b0c5-e5e631276dc0</OwnerGUID>
      </Event>
    </Events>
    <TotalPieces>1</TotalPieces>
    <TotalWeight Unit="kg">0.00</TotalWeight>
    <TotalVolume Unit="m3">76.181299313332</TotalVolume>
    <TotalVolumeWeight Unit="vlb">28005.22985510987928137183</TotalVolumeWeight>
    <ChargeableWeight Unit="kg">12702.95859302662938716821</ChargeableWeight>
    <TotalValue Currency="USD">0.00</TotalValue>
    <OriginPort Code="SHA">
      <Country Code="CN">CN</Country>
      <Method>Air</Method>
      <Method>Ocean</Method>
      <Method>Ground</Method>
      <Method>Mail</Method>
      <Name>Shanghai, China</Name>
      <Subdivision></Subdivision>
      <Remarks>subdivision-31</Remarks>
    </OriginPort>
    <DestinationPort Code="LAX">
      <Country Code="US">USA</Country>
      <Method>Air</Method>
      <Method>Ocean</Method>
      <Method>Ground</Method>
      <Method>Mail</Method>
      <Name>Los Angeles, CA</Name>
      <Subdivision>CA</Subdivision>
    </DestinationPort>
    <DeliveryPort Code="CHI">
      <Country Code="US">USA</Country>
      <Method>Air</Method>
      <Method>Ocean</Method>
      <Method>Ground</Method>
      <Name>Chicago</Name>
      <Subdivision>IL</Subdivision>
    </DeliveryPort>
    <Status>Loaded</Status>
    <LayoutType>House</LayoutType>
    <Direction>Incoming</Direction>
    <EstimatedArrivalDate>2020-01-28T16:27:02-05:00</EstimatedArrivalDate>
    <ActualArrivalDate>2019-12-26T16:29:59-05:00</ActualArrivalDate>
    <EstimatedDepartureDate>2020-01-14T16:27:02-05:00</EstimatedDepartureDate>
    <ActualDepartureDate>2019-12-26T16:29:59-05:00</ActualDepartureDate>
    <BillingClient GUID="186510fc-e1c4-4b8b-a0c4-99363b15bd41">
      <Type>Client</Type>
      <Name>Cnee</Name>
      <CreatedOn>2018-05-08T13:14:48-05:00</CreatedOn>
      <Address>
        <Street>TEST STREET NUMBER</Street>
        <City>Red Bank</City>
        <State>NJ</State>
        <ZipCode>07701</ZipCode>
        <Country Code="US">USA</Country>
        <Description>Default Shipping Address</Description>
      </Address>
      <BillingAddress>
        <Street>TEST STREET NUMBER</Street>
        <City>Red Bank</City>
        <State>NJ</State>
        <ZipCode>07701</ZipCode>
        <Country Code="US">USA</Country>
        <Description>Default Billing Address</Description>
      </BillingAddress>
      <AccountNumber>CX10136</AccountNumber>
      <Balance Currency="USD">114517.15998894054443</Balance>
      <TransactionDueDays>15</TransactionDueDays>
      <PaymentTerms>
        <Description>Net 15</Description>
        <NetDueDays>15</NetDueDays>
        <DiscountPercentage>0.00</DiscountPercentage>
        <DiscountPaidDays>0</DiscountPaidDays>
      </PaymentTerms>
      <IsPrepaid>false</IsPrepaid>
    </BillingClient>
    <ExecutedPlace>EL Segundo</ExecutedPlace>
    <MasterGUID>99999999-a096-4673-a76c-3e629da72bc9</MasterGUID>
    <LastDateOfDeparture>2019-12-26T16:27:02-05:00</LastDateOfDeparture>
    <SpottingDate>2019-12-26T16:27:02-05:00</SpottingDate>
    <PickupDate>2019-12-26T16:27:02-05:00</PickupDate>
    <CutoffDate>2019-12-26T16:27:02-05:00</CutoffDate>
    <MasterWayBillNumber>MBL4978OI</MasterWayBillNumber>
    <MasterNumber>MBL4978OI</MasterNumber>
    <HasAttachments>true</HasAttachments>
    <ShipmentOptions>
      <IsShipperConsigneeRelated>false</IsShipperConsigneeRelated>
      <IsRoutedTransaction>true</IsRoutedTransaction>
      <IsAirWayBillDetailExpanded>false</IsAirWayBillDetailExpanded>
      <IsBillOfLadingDetailExpanded>false</IsBillOfLadingDetailExpanded>
      <IsCargoManifestDetailExpanded>true</IsCargoManifestDetailExpanded>
      <FillDocsWithAccountingInfo>true</FillDocsWithAccountingInfo>
      <AirWayBillWeightUnit>Both</AirWayBillWeightUnit>
      <BillOfLadingWeightUnit>Both</BillOfLadingWeightUnit>
      <ShowContainerDetails>false</ShowContainerDetails>
      <ShowWHRsInCargoManifest>false</ShowWHRsInCargoManifest>
      <ShowDimensionsInAWB>true</ShowDimensionsInAWB>
      <ShowDimensionsDetailedInAWB>false</ShowDimensionsDetailedInAWB>
      <UseNotesAsMarksNumbersInBL>false</UseNotesAsMarksNumbersInBL>
      <BillOfLadingGroupSimilarItems>false</BillOfLadingGroupSimilarItems>
    </ShipmentOptions>
    <IsLessThanFullLoad>true</IsLessThanFullLoad>
    <Service>PortToPort</Service>
    <IsOnline>false</IsOnline>
    <IsLiquidated>false</IsLiquidated>
    <CreatedFrom>
      <GUID>c4a7f6eb-0695-4376-8994-7e18f0b2912b</GUID>
      <Type>OceanBooking</Type>
      <Number>EWL24195</Number>
    </CreatedFrom>
    <TotalContainers>1</TotalContainers>
    <Containers40Foot>1</Containers40Foot>
    <OceanShipmentInfo>
      <BillOfLadingNumber>SH19120796</BillOfLadingNumber>
      <VesselName>YM MOVEMENT</VesselName>
      <PlaceOfDeliveryByOnCarrier>Chicago</PlaceOfDeliveryByOnCarrier>
    </OceanShipmentInfo>
  </OceanShipment>
</Shipments>     

【问题讨论】:

【参考方案1】:

我使用来自 msdn 的 xsd.exe 实用程序来创建类。我必须同时下载您发布的架构和 common.xsd(相同的路径,不同文件名的发布架构)。然后将生成的类粘贴到程序中。然后使用以下代码

        const string FILENAME = @"c:\temp\test.xml";
        static void Main(string[] args)
        
            XmlReader reader = XmlReader.Create(FILENAME);
            XmlSerializer serializer = new XmlSerializer(typeof(ShipmentList));
            ShipmentList shipmentList = (ShipmentList)serializer.Deserialize(reader);
        

【讨论】:

JD 你美丽了不起的人类。我对 C# 的理解有点初级,我试图将 ShipmentType 用于我的对象,事后看来(并查看您的代码)实际上没有任何意义。再次感谢JD,您是真正的绅士和学者。由于我的编程能力有限,我非常犹豫在这里发帖,并且为此苦苦挣扎了两个星期。我很高兴您回答了我的问题,并平息了我对更频繁地寻求帮助的恐惧! 在架构中有这一行:。当 xsd.exe 运行时,它会创建一个类,如下所示:[System.Xml.Serialization.XmlRootAttribute("Shipments", Namespace="magaya.com/XMLSchema/V1", IsNullable=false)] public partial class ShipmentList code here 。 XmlRootAttribute 将 xml 标记名称映射到类名称。

以上是关于将 Web 服务 API 中的 XML 字符串反序列化为 C# 对象的主要内容,如果未能解决你的问题,请参考以下文章

如何处理JSON中的特殊字符

如何将 XML 字符串发布到 Web 服务以进行测试? [关闭]

wininet api 将文件发送到 http web 服务

Odoo Web Service API

如何在 asp.net 中创建 RESTful Web 服务?

什么是通过iOS中的Web服务url传递带有xml标签的参数的更好方法,目标c