1

我正在尝试使用 Linq to Entities 从 SQL 数据源创建 XML 文档。我使用 XSD 为 XML 文档创建类。XML结构如下(部分列举):

<?xml version="1.0" encoding="UTF-8"?>
<ExecutedShipment>
   <ExecutedShipmentIdentifier>098098</ExecutedShipmentIdentifier>
   <ShipDirection>Outbound</ShipDirection>
   <FreightTerms>Pre-Paid</FreightTerms>
   <CarrierSCAC>SEFL</CarrierSCAC>
   <EquipmentType>LTL</EquipmentType>
   <CarrierTrackingNumber>100283534</CarrierTrackingNumber>
   <LoadList>
      <Load>
         <ExecutedLoadIdentifier>098098</ExecutedLoadIdentifier>
         <Schedule>
            <ActualShipDate timezone="EST">2004-03-19T15:35:00</ActualShipDate>
         </Schedule>
         <PickupSequenceNumber>1</PickupSequenceNumber>
         <DropoffSequenceNumber>1</DropoffSequenceNumber>
         <ShipFrom>
            <SiteIdentifier>123456</SiteIdentifier>
         <FullName>ABC Inc.</FullName>
             <Location>
               <Address1>354 Main Street</Address1>
               <Postal>01234</Postal>
               <Country>US</Country>
            </Location>
        </ShipFrom>

这是我用来填充类的代码,以便我可以将其序列化为 XML 文件:

    List<ExecutedShipment> Shipments = new List<ExecutedShipment>();

    Shipments = (
                from sh in ShipData.shiphead
                where sh.shipdate >= Yesterday && sh.shipdate <= Yesterday
                select new ExecutedShipment
                {
                    ExecutedShipmentIdentifier = SqlFunctions.StringConvert((double)sh.packnum),
                    ShipDirection = "Outbound",
                    FreightTerms = "Pre-Paid",
                    CarrierSCAC = sh.shipviacode,
                    EquipmentType = "LTL",
                    CarrierTrackingNumber = sh.trackingnumber,
                    LoadList = new ExecutedShipmentLoadListLoad
                               { 
                                   ExecutedLoadIdentifier = SqlFunctions.StringConvert((double)sh.packnum),
                                   DropoffSequenceNumber = "TEST"
                                   //Schedule = (new ExecutedShipmentLoadListLoadScheduleActualShipDate
                                   //           {
                                   //               timezone = "EST",
                                   //               Value = "0000-00-00"  //sh.shipdate
                                   //           }).ToArray()
                               }

                }).ToList<ExecutedShipment>();

代码的第一部分工作正常。当我到达“LoadList”时,我遇到了问题。在 Visual Studio 中,“新的 ExecutedShipmentLoadListLoad”带有红色下划线,错误是无法将类型“ExecutedShipmentLoadListLoad”隐式转换为“ExecutedShipmentLoadListLoad[]”。'LoadList' 是一个数组,但我没有尝试将其转换为相同类型的数组。那么,一般来说,您如何引用 XSD 生成的类中的子元素并为其赋值?

4

1 回答 1

1

您只需创建一个数组并将实例放入数组声明中,如下所示:

// Start array declaration.
LoadList = new [] {

    // First element.
    new ExecutedShipmentLoadListLoad { 
        ExecutedLoadIdentifier = SqlFunctions.StringConvert((double)sh.packnum),
        DropoffSequenceNumber = "TEST"
        //Schedule = (new ExecutedShipmentLoadListLoadScheduleActualShipDate
        //           {
        //               timezone = "EST",
        //               Value = "0000-00-00"  //sh.shipdate
        //           }).ToArray()
    }

    // Other elements here.

// End array declaration
}
于 2012-08-24T16:39:20.737 回答