使用Linq to Entities和XSD创建嵌套XML文档

时间:2012-08-22 23:14:38

标签: c# xml linq xsd entities

我正在尝试使用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中,“new ExecutedShipmentLoadListLoad”以红色下划线,错误是无法将类型'ExecutedShipmentLoadListLoad'隐式转换为'ExecutedShipmentLoadListLoad []'。 'LoadList'是一个数组,但我试图转换为相同类型的数组的任何东西都有效。那么,一般来说,如何在XSD生成的类中引用和赋值给子元素?

1 个答案:

答案 0 :(得分: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
}