Message=The string '2015-09-06 00:00:00' is not a valid AllXsd value

时间:2015-09-14 16:15:40

标签: c# .net xml entity-framework xml-serialization

I am trying to write data from XML to db using entity framework. I've read some posts with the same problem but I couldn't get it to work properly. Previously I had following attribute:

[XmlElement("DateTime")]
public Nullable<System.DateTime> DateTime { get; set; }

And the data was:

2015-09-06 00:00:00

So I was getting error:

Message=The string '2015-09-06 00:00:00' is not a valid AllXsd value.

So, I've found solution:

private DateTime? _dateTime;

[XmlIgnore]
public DateTime? DateTime
{
    get { return _dateTime; }
    set { _dateTime = value; }
}

[XmlAttribute("DateTime")]
public string DateTimeTimeString
{
    get
    {
        return _dateTime.HasValue ? XmlConvert.ToString(_dateTime.Value, XmlDateTimeSerializationMode.Unspecified)
        : string.Empty;
    }
    set
    {
        _dateTime = !string.IsNullOrEmpty(value) ? XmlConvert.ToDateTime(value, XmlDateTimeSerializationMode.Unspecified) : (DateTime?)null;
    }
}

I do not really understand what exactly is done here, but it just skips the values and the date columns are just not populated.

I've tried to debut it and saw that it just goes into the public DateTime? DateTime section so obviously the value is empty ... How to get it working?

Sample xml:

<?xml versi encoding="UTF-8"?>
<root>
   <success>true</success>
   <data>
      <item>
         <Ref>074e9b7c-5486-11e5-ad08-005056801333</Ref>
         <DateTime>2015-09-06 00:00:00</DateTime>
         <PreferredDeliveryDate>0001-01-01 00:00:00</PreferredDeliveryDate>
         <Weight>0.30</Weight>
         <SeatsAmount>1</SeatsAmount>
         <IntDocNumber>20450004714285</IntDocNumber>
         <Cost>769.00</Cost>
         <CitySender>db5c88d0-391c-11dd-90d9-001a92567626</CitySender>
         <CityRecipient>c873836a-b02c-11e4-a77a-005056887b8d</CityRecipient>
         <State>00000000-0000-0000-0000-000000000000</State>
         <SenderAddress>9221f87a-1c7b-11e4-acce-0050568002cf</SenderAddress>
         <RecipientAddress>c873837c-b02c-11e4-a77a-005056887b8d</RecipientAddress>
         <CostOnSite>29.00</CostOnSite>
         <PayerType>Recipient</PayerType>
         <PaymentMethod>Cash</PaymentMethod>
         <AfterpaymentOnGoodsCost>0.00</AfterpaymentOnGoodsCost>
         <CargoType>Cargo</CargoType>
         <PackingNumber />
         <AdditionalInformation />
         <Number />
         <Posted />
         <DeletionMark />
         <Route />
         <EWNumber />
         <Description />
         <SaturdayDelivery />
         <ExpressWaybill />
         <CarCall />
         <ServiceType />
         <DeliveryDateFrom />
         <Vip />
         <LastModificationDate />
         <ReceiptDate />
         <LoyaltyCard />
         <Sender />
         <ContactSender />
         <SendersPhone />
         <Recipient />
         <ContactRecipient />
         <RecipientsPhone />
         <Redelivery />
         <SaturdayDeliveryString />
         <Note />
         <ThirdPerson />
         <Forwarding />
         <NumberOfFloorsLifting />
         <StatementOfAcceptanceTransferCargoID />
         <StateId>10</StateId>
         <Printed>0</Printed>
         <ChangedDataEW>0</ChangedDataEW>
         <Fulfillment>0</Fulfillment>
         <EstimatedDeliveryDate>2015-09-10 00:00:00</EstimatedDeliveryDate>
         <DateLastUpdatedStatus>2015-09-13 21:00:32</DateLastUpdatedStatus>
         <CreateTime>2015-09-06 13:57:13</CreateTime>
         <ScanSheetNumber />
         <InfoRegClientBarcodes />
         <StatePayId>0</StatePayId>
         <StatePayName />
      </item>
   </data>
   <errors />
   <warnings />
   <info />
</root>

0 个答案:

没有答案