1

I'm trying to create and XML document from a SQL datasource using Linq to Entities. I used XSD to create the classes for the XML document. The XML structure is as follows (partial listing):

<?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>

Here's the code I'm using to populate the class so that I can serialize it to an XML file:

    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>();

The first part of the code works fine. It's when I get to 'LoadList' that I have a problem. In Visual Studio the 'new ExecutedShipmentLoadListLoad' is underlined in red and the error is Cannot implicitly convert type 'ExecutedShipmentLoadListLoad' to 'ExecutedShipmentLoadListLoad[]'. 'LoadList' is an array, but nothing I've tried to convert to an array of the same type has worked. So, generally speaking, how do you reference and assign values to sub-elements within the XSD generated class?

1 Answer 1

1

You simply have to create an array and put the instances in the array declaration, like so:

// 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
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.