簡體   English   中英

InnerException:將字符串轉換為DateTime時。 在將每個變量放入DateTime對象之前,分析字符串以對日期進行日期解析

[英]InnerException:When converting a string to DateTime. parse the string to date the date before putting each variable into the DateTime object

我正在嘗試反序列化從API調用返回的XML,但得到的是“ InnerException:將字符串轉換為DateTime時。在將每個變量放入DateTime對象之前,分析字符串的日期為日期”

XML看起來像這樣

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<agentInventory xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:N1="demo.org.uk/demo/CustomsStatus" xmlns:N2="demo.org.uk/demo/UnLocation" xmlns:N3="demo.org.uk/demo/AirCarrier" xmlns="demo.org.uk/demo/AgentInventory">
  <shed>ADX</shed>
  <arrivalPort>
    <N2:iataPortCode>LHR</N2:iataPortCode>
      </arrivalPort>
  <masterAirwayBillPrefix>125</masterAirwayBillPrefix>
  <masterAirwayBillNumber>28121101</masterAirwayBillNumber>
  <nominatedAgent>DRB</nominatedAgent>
  <originPort>
   <N2:iataPortCode>BOS</N2:iataPortCode>
  </originPort>
  <destinationPort>
    <N2:iataPortCode>LHR</N2:iataPortCode>
  </destinationPort>
  <airCarrier>
    <N3:carrierCode>BA</N3:carrierCode>
  </airCarrier>
  <flightNumber>235</flightNumber>
  <flightDate>2012-02-09T00:00:00Z</flightDate>
  <npx>10</npx>
  <npr>0</npr>
 <grossWeight>123.0</grossWeight>
  <goodsDescription>BOOKS</goodsDescription>
  <sdc>T</sdc>
  <status1Set></status1Set>
  <status2Set>false</status2Set>
  <ccsCreationTime></ccsCreationTime>
  <customsSummaryText />
  <customsSummaryTime></customsSummaryTime>
  <agentReference />
  <isErtsPreArrival>false</isErtsPreArrival>
  <isAgentPreArrival>false</isAgentPreArrival>
  <isDeleted>false</isDeleted>
  <finalised></finalised>
  <createdOn>2012-01-24T11:50:40.86Z</createdOn>
  <modifiedOn>2012-02-09T09:51:26.617Z</modifiedOn>
  </agentInventory>

用於反序列化XML的代碼是

 if (storeXmlInventoryReturnData != null)
        {

            //DeSerialize XML from storeXmlInventoryReturnData variable
            agentInventory myInventoryResponse = null;

           XmlSerializer xmlSerializer = new XmlSerializer(typeof(agentInventory));                             
          myInventoryResponse =  (
               agentInventory)xmlSerializer.Deserialize(new StringReader(storeXmlInventoryReturnData)

               );

            Console.WriteLine(
             @"\n\n\n INVENTORY RETURN DATA FOR AWB: {0}-{1} \n\n 
                          Destination Port: {2} \n 
                          Arrival Port: {3} \n 
                          Carrier: {4} \n 
                          Flight No: {5} \n 
                          Flight Date: {6} \n 
                          Customers Status: {7} \n 
                          NPX: {8} \n
                          NPR {9} \n
                          SDC Code: {10}                              
                          \n\n Hit any key to exit...."
                ,

                myInventoryResponse.masterAirwayBillPrefix,
               myInventoryResponse.masterAirwayBillNumber,
                myInventoryResponse.destinationPort,
                myInventoryResponse.arrivalPort,
                myInventoryResponse.airCarrier,
                myInventoryResponse.flightNumber,
               myInventoryResponse.flightDate,
               myInventoryResponse.customsStatus,
                myInventoryResponse.npx,
                myInventoryResponse.npr,
                myInventoryResponse.sdc,
               myInventoryResponse.grossWeight,
                myInventoryResponse.goodsDescription
                    );
            Console.ReadLine();
        }
        else
        {
            Console.Write("No data returned");
        }

    }

拋出異常。 myInventoryResponse =(agentInventory)xmlSerializer.Deserialize(new StringReader(storeXmlInventoryReturnData));

我想我需要將myInventoryResponse.flightDate轉換為DateTime對象,但我無所適從如何實現這一目標?

namespace FreightSolutions {
using System;
using System.Diagnostics;
using System.Xml.Serialization;
using System.Collections;
using System.Xml.Schema;
using System.ComponentModel;
using System.Collections.Generic;


[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType=true, Namespace="asm.org.uk/Sequoia/AgentInventory")]
[System.Xml.Serialization.XmlRootAttribute(Namespace="demo.org.uk/demo/AgentInventory", IsNullable=false)]
public partial class agentInventory {

    private string shedField;

    private unLocation arrivalPortField;

    private string masterAirwayBillPrefixField;

    private string masterAirwayBillNumberField;

    private string houseAirwayBillNumberField;

    private string splitReferenceNumberField;

    private string nominatedAgentField;

    private unLocation originPortField;

    private unLocation destinationPortField;

    private airCarrier airCarrierField;

    private string flightNumberField;

    private System.DateTime flightDateField;

    private bool flightDateFieldSpecified;

    private string npxField;

    private string nprField;

    private float grossWeightField;

    private bool grossWeightFieldSpecified;

    private string goodsDescriptionField;

    private string sdcField;

    private System.DateTime status1SetField;

    private bool status1SetFieldSpecified;

    private bool status2SetField;

    private bool status2SetFieldSpecified;

    private System.DateTime ccsCreationTimeField;

    private bool ccsCreationTimeFieldSpecified;

    private customsStatus customsStatusField;

    private string customsSummaryTextField;

    private System.DateTime customsSummaryTimeField;

    private bool customsSummaryTimeFieldSpecified;

    private string agentReferenceField;

    private bool isErtsPreArrivalField;

    private bool isErtsPreArrivalFieldSpecified;

    private bool isAgentPreArrivalField;

    private bool isAgentPreArrivalFieldSpecified;

    private bool isDeletedField;

    private bool isDeletedFieldSpecified;

    private System.DateTime finalisedField;

    private bool finalisedFieldSpecified;

    private System.DateTime createdOnField;

    private System.DateTime modifiedOnField;

    private bool modifiedOnFieldSpecified;

    public agentInventory() {
        this.customsStatusField = new customsStatus();
        this.airCarrierField = new airCarrier();
        this.destinationPortField = new unLocation();
        this.originPortField = new unLocation();
        this.arrivalPortField = new unLocation();
    }

    public string shed {
        get {
            return this.shedField;
        }
        set {
            this.shedField = value;
        }
    }

    public unLocation arrivalPort {
        get {
            return this.arrivalPortField;
        }
        set {
            this.arrivalPortField = value;
        }
    }

    public string masterAirwayBillPrefix {
        get {
            return this.masterAirwayBillPrefixField;
        }
        set {
            this.masterAirwayBillPrefixField = value;
        }
    }

    public string masterAirwayBillNumber {
        get {
            return this.masterAirwayBillNumberField;
        }
        set {
            this.masterAirwayBillNumberField = value;
        }
    }

    public string houseAirwayBillNumber {
        get {
            return this.houseAirwayBillNumberField;
        }
        set {
            this.houseAirwayBillNumberField = value;
        }
    }

    public string splitReferenceNumber {
        get {
            return this.splitReferenceNumberField;
        }
        set {
            this.splitReferenceNumberField = value;
        }
    }

    public string nominatedAgent {
        get {
            return this.nominatedAgentField;
        }
        set {
            this.nominatedAgentField = value;
        }
    }

    public unLocation originPort {
        get {
            return this.originPortField;
        }
        set {
            this.originPortField = value;
        }
    }

    public unLocation destinationPort {
        get {
            return this.destinationPortField;
        }
        set {
            this.destinationPortField = value;
        }
    }

    public airCarrier airCarrier {
        get {
            return this.airCarrierField;
        }
        set {
            this.airCarrierField = value;
        }
    }

    public string flightNumber {
        get {
            return this.flightNumberField;
        }
        set {
            this.flightNumberField = value;
        }
    }

    public System.DateTime flightDate {
        get {
            return this.flightDateField;
        }
        set {
            this.flightDateField = value;
        }
    }

    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool flightDateSpecified {
        get {
            return this.flightDateFieldSpecified;
        }
        set {
            this.flightDateFieldSpecified = value;
        }
    }

    [System.Xml.Serialization.XmlElementAttribute(DataType="integer")]
    public string npx {
        get {
            return this.npxField;
        }
        set {
            this.npxField = value;
        }
    }

    [System.Xml.Serialization.XmlElementAttribute(DataType="integer")]
    public string npr {
        get {
            return this.nprField;
        }
        set {
            this.nprField = value;
        }
    }

    public float grossWeight {
        get {
            return this.grossWeightField;
        }
        set {
            this.grossWeightField = value;
        }
    }

    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool grossWeightSpecified {
        get {
            return this.grossWeightFieldSpecified;
        }
        set {
            this.grossWeightFieldSpecified = value;
        }
    }

    public string goodsDescription {
        get {
            return this.goodsDescriptionField;
        }
        set {
            this.goodsDescriptionField = value;
        }
    }

    public string sdc {
        get {
            return this.sdcField;
        }
        set {
            this.sdcField = value;
        }
    }

    public System.DateTime status1Set {
        get {
            return this.status1SetField;
        }
        set {
            this.status1SetField = value;
        }
    }

    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool status1SetSpecified {
        get {
            return this.status1SetFieldSpecified;
        }
        set {
            this.status1SetFieldSpecified = value;
        }
    }

    public bool status2Set {
        get {
            return this.status2SetField;
        }
        set {
            this.status2SetField = value;
        }
    }

    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool status2SetSpecified {
        get {
            return this.status2SetFieldSpecified;
        }
        set {
            this.status2SetFieldSpecified = value;
        }
    }

    public System.DateTime ccsCreationTime {
        get {
            return this.ccsCreationTimeField;
        }
        set {
            this.ccsCreationTimeField = value;
        }
    }

    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool ccsCreationTimeSpecified {
        get {
            return this.ccsCreationTimeFieldSpecified;
        }
        set {
            this.ccsCreationTimeFieldSpecified = value;
        }
    }

    public customsStatus customsStatus {
        get {
            return this.customsStatusField;
        }
        set {
            this.customsStatusField = value;
        }
    }

    public string customsSummaryText {
        get {
            return this.customsSummaryTextField;
        }
        set {
            this.customsSummaryTextField = value;
        }
    }

    public System.DateTime customsSummaryTime {
        get {
            return this.customsSummaryTimeField;
        }
        set {
            this.customsSummaryTimeField = value;
        }
    }

    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool customsSummaryTimeSpecified {
        get {
            return this.customsSummaryTimeFieldSpecified;
        }
        set {
            this.customsSummaryTimeFieldSpecified = value;
        }
    }

    public string agentReference {
        get {
            return this.agentReferenceField;
        }
        set {
            this.agentReferenceField = value;
        }
    }

    public bool isErtsPreArrival {
        get {
            return this.isErtsPreArrivalField;
        }
        set {
            this.isErtsPreArrivalField = value;
        }
    }

    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool isErtsPreArrivalSpecified {
        get {
            return this.isErtsPreArrivalFieldSpecified;
        }
        set {
            this.isErtsPreArrivalFieldSpecified = value;
        }
    }

    public bool isAgentPreArrival {
        get {
            return this.isAgentPreArrivalField;
        }
        set {
            this.isAgentPreArrivalField = value;
        }
    }

    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool isAgentPreArrivalSpecified {
        get {
            return this.isAgentPreArrivalFieldSpecified;
        }
        set {
            this.isAgentPreArrivalFieldSpecified = value;
        }
    }

    public bool isDeleted {
        get {
            return this.isDeletedField;
        }
        set {
            this.isDeletedField = value;
        }
    }

    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool isDeletedSpecified {
        get {
            return this.isDeletedFieldSpecified;
        }
        set {
            this.isDeletedFieldSpecified = value;
        }
    }

    public System.DateTime finalised {
        get {
            return this.finalisedField;
        }
        set {
            this.finalisedField = value;
        }
    }

    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool finalisedSpecified {
        get {
            return this.finalisedFieldSpecified;
        }
        set {
            this.finalisedFieldSpecified = value;
        }
    }

    public System.DateTime createdOn {
        get {
            return this.createdOnField;
        }
        set {
            this.createdOnField = value;
        }
    }

    public System.DateTime modifiedOn {
        get {
            return this.modifiedOnField;
        }
        set {
            this.modifiedOnField = value;
        }
    }

    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool modifiedOnSpecified {
        get {
            return this.modifiedOnFieldSpecified;
        }
        set {
            this.modifiedOnFieldSpecified = value;
        }
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="demo.org.uk/demo/UnLocation")]
public partial class unLocation {

    private string itemField;

    private ItemChoiceType itemElementNameField;

    [System.Xml.Serialization.XmlElementAttribute("iataPortCode", typeof(string))]
    [System.Xml.Serialization.XmlElementAttribute("oceanPortCode", typeof(string))]
    [System.Xml.Serialization.XmlChoiceIdentifierAttribute("ItemElementName")]
    public string Item {
        get {
            return this.itemField;
        }
        set {
            this.itemField = value;
        }
    }

    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public ItemChoiceType ItemElementName {
        get {
            return this.itemElementNameField;
        }
        set {
            this.itemElementNameField = value;
        }
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="demo.org.uk/demo/UnLocation", IncludeInSchema=false)]
public enum ItemChoiceType {

    /// <remarks/>
    iataPortCode,

    /// <remarks/>
    oceanPortCode,
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="demo.org.uk/demo/CustomsStatus")]
public partial class customsStatus {

    private string codeField;

    private string statusTextField;

    public string code {
        get {
            return this.codeField;
        }
        set {
            this.codeField = value;
        }
    }

    public string statusText {
        get {
            return this.statusTextField;
        }
        set {
            this.statusTextField = value;
        }
    }
}

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.34234")]
[System.SerializableAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="demo.org.uk/demo/AirCarrier")]
public partial class airCarrier {

    private string carrierCodeField;

    public string carrierCode {
        get {
            return this.carrierCodeField;
        }
        set {
            this.carrierCodeField = value;
        }
    }
}

}

為了我自己的頭腦,我嘗試使提供的XML與您的類和可為null的DateTime字段一起使用。

為了使它們起作用,請用以下鏈接的屬性替換以下屬性。 DateTime對象的默認值為DateTime.MinDate ,因此,如果傳入的值為null或為空,則該屬性將始終具有DateTime.MinDate的值,為此,如果您的代碼需要執行以下操作,則可能需要進行其他驗證/更改:迎合這一點。

  #region XML Nullable helper properties
  [XmlElement(ElementName = "status1Set")]
  public object status1SetXml
  {
    get
    {
      // Return the main property itself here and not the field to ensure that if there are changes made to the getter,
      // the resulting XML will always have the right value
      return status1Set;
    }
    set
    {
      // value passed in is of type XmlNode[], so convert and get the XmlNode text, which should be the DateTime we want
      // !!ASSUMPTION!! That it will alwas be XmlNode[1] type
      XmlNode[] inputValue = value as XmlNode[];
      if (inputValue != null)
      {
        DateTime.TryParse(Convert.ToString(inputValue[0].InnerText), out status1SetField);
      }
    }
  }

  [XmlElement(ElementName="ccsCreationTime")]
  public object ccsCreationTimeXml
  {
    get
    {
      // Return the main property itself here and not the field to ensure that if there are changes made to the getter,
      // the resulting XML will always have the right value
      return ccsCreationTime;
    }
    set
    {
      // value passed in is of type XmlNode[], so convert and get the XmlNode text, which should be the DateTime we want
      // !!ASSUMPTION!! That it will alwas be XmlNode[1] type
      XmlNode[] inputValue = value as XmlNode[];
      if (inputValue != null)
      {
        DateTime.TryParse(Convert.ToString(inputValue[0].InnerText), out ccsCreationTimeField);
      }
    }
  }

  [XmlElement(ElementName = "customsSummaryTime")]
  public object customsSummaryTimeXml
  {
    get
    {
      // Return the main property itself here and not the field to ensure that if there are changes made to the getter,
      // the resulting XML will always have the right value
      return customsSummaryTime;
    }
    set
    {
      // value passed in is of type XmlNode[], so convert and get the XmlNode text, which should be the DateTime we want
      // !!ASSUMPTION!! That it will alwas be XmlNode[1] type
      XmlNode[] inputValue = value as XmlNode[];
      if (inputValue != null)
      {
        DateTime.TryParse(Convert.ToString(inputValue[0].InnerText), out customsSummaryTimeField);
      }
    }
  }

  [XmlElement(ElementName = "finalised")]
  public object finalisedXml
  {
    get
    {
      // Return the main property itself here and not the field to ensure that if there are changes made to the getter,
      // the resulting XML will always have the right value
      return finalised;
    }
    set
    {
      // value passed in is of type XmlNode[], so convert and get the XmlNode text, which should be the DateTime we want
      // !!ASSUMPTION!! That it will alwas be XmlNode[1] type
      XmlNode[] inputValue = value as XmlNode[];
      if (inputValue != null)
      {
        DateTime.TryParse(Convert.ToString(inputValue[0].InnerText), out finalisedField);
      }
    }
  }
  #endregion

  #region Non XML properties
  [XmlIgnore]
  public System.DateTime status1Set
  {
    get { return this.status1SetField; }
    set { this.status1SetField = value; }
  }

  [XmlIgnore]
  public System.DateTime ccsCreationTime
  {
    get { return this.ccsCreationTimeField; }
    set { this.ccsCreationTimeField = value; }
  }

  [XmlIgnore]
  public System.DateTime customsSummaryTime
  {
    get { return this.customsSummaryTimeField; }
    set { this.customsSummaryTimeField = value; }
  }

  [XmlIgnore]
  public System.DateTime finalised
  {
    get { return this.finalisedField; }
    set { this.finalisedField = value; }
  }
  #endregion

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM