WebApi中序列化到XML默认访问器时出错

时间:2018-07-12 21:41:48

标签: c# .net json asp.net-web-api serialization

我正在尝试从WCF服务中执行Web api,并且多个帖子都可以正常工作,但是对于特定的帖子,我遇到了麻烦。这是来自API的代码:

[HttpPost]
[DeflateCompression]
[ActionName("Sell")]
public OTA_HotelResNotifRS Sell([FromBody]OTA_HotelResNotifRQ OTA_HotelResNotifRQ)
{
    ClientProviderHotel client = new ClientProviderHotel();
    return client.Hotel_Sell(OTA_HotelResNotifRQ);
}

这是来自Hotel_Sell方法的代码:

public OTA_HotelResNotifRS Hotel_Sell(OTA_HotelResNotifRQ Value)
{
    var objBRE = new Engine();


    string MarketCode;


    Timerwatch timerHotel_Sell = new Timerwatch("Hotel_Sell", "");
    timerHotel_Sell.Start();
    ExternalValues External = new ExternalValues();

    XDocument ValueDocument = XDocument.Parse(Serializer.SerializeToString<OTA_HotelResNotifRQ>(Value));

此时:

    XDocument ValueDocument = XDocument.Parse(Serializer.SerializeToString<OTA_HotelResNotifRQ>(Value));

我调用以下方法将对象序列化为XML字符串:

/// <summary>
/// Serialize Data to XML
/// </summary>
/// <typeparam name="T"> object Base</typeparam>
/// <param name="Value"> Object</param>
/// <returns>Data convert to XML</returns>
public static String SerializeToString<T>(T Value) 
    where T : class
{
   XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
   StringWriter stringWriter = new StringWriter();
   xmlSerializer.Serialize(stringWriter, Value);

   return stringWriter.ToString();
}

但是有以下错误:

  

System.InvalidOperationException:“生成XML文档时出错。”

     

InvalidOperationException:您必须在中实现默认访问器   Newtonsoft.Json.Linq.JObject,因为它继承自ICollection。

如果我使用WCF执行该过程,则不会出现错误。

对象将在下面描述:

    /// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.opentravel.org/OTA/2003/05")]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.opentravel.org/OTA/2003/05", IsNullable = false)]
public partial class OTA_HotelResNotifRQ
{

    private SourceType[] pOSField;

    private UniqueID_Type messageIDField;

    private HotelReservationsType hotelReservationsField;

    private string echoTokenField;

    private System.DateTime timeStampField;

    private bool timeStampFieldSpecified;

    private OTA_AirAvailRQTarget targetField;

    private bool targetFieldSpecified;

    private string targetNameField;

    private decimal versionField;

    private string transactionIdentifierField;

    private string sequenceNmbrField;

    private OTA_AirAvailRQTransactionStatusCode transactionStatusCodeField;

    private bool transactionStatusCodeFieldSpecified;

    private string primaryLangIDField;

    private string altLangIDField;

    private bool retransmissionIndicatorField;

    private bool retransmissionIndicatorFieldSpecified;

    private string correlationIDField;

    private TransactionActionType resStatusField;

    private bool resStatusFieldSpecified;

    private string holdDurationField;

    /// <remarks/>
    [System.Xml.Serialization.XmlArrayItemAttribute("Source", IsNullable = false)]
    public SourceType[] POS
    {
        get
        {
            return this.pOSField;
        }
        set
        {
            this.pOSField = value;
        }
    }

    /// <remarks/>
    public UniqueID_Type MessageID
    {
        get
        {
            return this.messageIDField;
        }
        set
        {
            this.messageIDField = value;
        }
    }

    /// <remarks/>
    public HotelReservationsType HotelReservations
    {
        get
        {
            return this.hotelReservationsField;
        }
        set
        {
            this.hotelReservationsField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string EchoToken
    {
        get
        {
            return this.echoTokenField;
        }
        set
        {
            this.echoTokenField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public System.DateTime TimeStamp
    {
        get
        {
            return this.timeStampField;
        }
        set
        {
            this.timeStampField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool TimeStampSpecified
    {
        get
        {
            return this.timeStampFieldSpecified;
        }
        set
        {
            this.timeStampFieldSpecified = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public OTA_AirAvailRQTarget Target
    {
        get
        {
            return this.targetField;
        }
        set
        {
            this.targetField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool TargetSpecified
    {
        get
        {
            return this.targetFieldSpecified;
        }
        set
        {
            this.targetFieldSpecified = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string TargetName
    {
        get
        {
            return this.targetNameField;
        }
        set
        {
            this.targetNameField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public decimal Version
    {
        get
        {
            return this.versionField;
        }
        set
        {
            this.versionField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string TransactionIdentifier
    {
        get
        {
            return this.transactionIdentifierField;
        }
        set
        {
            this.transactionIdentifierField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute(DataType = "nonNegativeInteger")]
    public string SequenceNmbr
    {
        get
        {
            return this.sequenceNmbrField;
        }
        set
        {
            this.sequenceNmbrField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public OTA_AirAvailRQTransactionStatusCode TransactionStatusCode
    {
        get
        {
            return this.transactionStatusCodeField;
        }
        set
        {
            this.transactionStatusCodeField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool TransactionStatusCodeSpecified
    {
        get
        {
            return this.transactionStatusCodeFieldSpecified;
        }
        set
        {
            this.transactionStatusCodeFieldSpecified = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute(DataType = "language")]
    public string PrimaryLangID
    {
        get
        {
            return this.primaryLangIDField;
        }
        set
        {
            this.primaryLangIDField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute(DataType = "language")]
    public string AltLangID
    {
        get
        {
            return this.altLangIDField;
        }
        set
        {
            this.altLangIDField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public bool RetransmissionIndicator
    {
        get
        {
            return this.retransmissionIndicatorField;
        }
        set
        {
            this.retransmissionIndicatorField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool RetransmissionIndicatorSpecified
    {
        get
        {
            return this.retransmissionIndicatorFieldSpecified;
        }
        set
        {
            this.retransmissionIndicatorFieldSpecified = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public string CorrelationID
    {
        get
        {
            return this.correlationIDField;
        }
        set
        {
            this.correlationIDField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public TransactionActionType ResStatus
    {
        get
        {
            return this.resStatusField;
        }
        set
        {
            this.resStatusField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool ResStatusSpecified
    {
        get
        {
            return this.resStatusFieldSpecified;
        }
        set
        {
            this.resStatusFieldSpecified = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute(DataType = "duration")]
    public string HoldDuration
    {
        get
        {
            return this.holdDurationField;
        }
        set
        {
            this.holdDurationField = value;
        }
    }
}q

此过程没有通过JSON.Net库进行显式序列化,当我发送发布请求时,仅在Postman中使用Json

0 个答案:

没有答案