Search code examples
c#web-servicessoapproxyxmlserializer

XML Serializer looses values of the object after serialization


I have a wsdl document 3rd party and try to serialize request to XML. I figured out that some values(like PaymentType), which were set to instance of the object are not serializing. Others do (like PersonName). What could be the reason of such problem and how to fix it? The problem is not in XML itself, while it's proxy generated. Looks there should be different reason on previous step or some setting.

Here is an example:

enter image description here

And XML classes of the 3rd party service.

[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.3056.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://fedex.com/ws/rate/v24")]
public partial class CustomsClearanceDetail {

    private BrokerDetail[] brokersField;

    private ClearanceBrokerageType clearanceBrokerageField;

    private bool clearanceBrokerageFieldSpecified;

    private CustomsOptionDetail customsOptionsField;

    private Party importerOfRecordField;

    private RecipientCustomsId recipientCustomsIdField;

    private Payment dutiesPaymentField;

    private InternationalDocumentContentType documentContentField;

    private bool documentContentFieldSpecified;

    private Money customsValueField;

    private FreightOnValueType freightOnValueField;

    private bool freightOnValueFieldSpecified;

    private Money insuranceChargesField;

    private bool partiesToTransactionAreRelatedField;

    private bool partiesToTransactionAreRelatedFieldSpecified;

    private CommercialInvoice commercialInvoiceField;

    private Commodity[] commoditiesField;

    private ExportDetail exportDetailField;

    private RegulatoryControlType[] regulatoryControlsField;

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Brokers")]
    public BrokerDetail[] Brokers {
        get {
            return this.brokersField;
        }
        set {
            this.brokersField = value;
        }
    }

    /// <remarks/>
    public ClearanceBrokerageType ClearanceBrokerage {
        get {
            return this.clearanceBrokerageField;
        }
        set {
            this.clearanceBrokerageField = value;
        }
    }

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

    /// <remarks/>
    public CustomsOptionDetail CustomsOptions {
        get {
            return this.customsOptionsField;
        }
        set {
            this.customsOptionsField = value;
        }
    }

    /// <remarks/>
    public Party ImporterOfRecord {
        get {
            return this.importerOfRecordField;
        }
        set {
            this.importerOfRecordField = value;
        }
    }

    /// <remarks/>
    public RecipientCustomsId RecipientCustomsId {
        get {
            return this.recipientCustomsIdField;
        }
        set {
            this.recipientCustomsIdField = value;
        }
    }

    /// <remarks/>
    public Payment DutiesPayment {
        get {
            return this.dutiesPaymentField;
        }
        set {
            this.dutiesPaymentField = value;
        }
    }

    /// <remarks/>
    public InternationalDocumentContentType DocumentContent {
        get {
            return this.documentContentField;
        }
        set {
            this.documentContentField = value;
        }
    }

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

    /// <remarks/>
    public Money CustomsValue {
        get {
            return this.customsValueField;
        }
        set {
            this.customsValueField = value;
        }
    }

    /// <remarks/>
    public FreightOnValueType FreightOnValue {
        get {
            return this.freightOnValueField;
        }
        set {
            this.freightOnValueField = value;
        }
    }

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

    /// <remarks/>
    public Money InsuranceCharges {
        get {
            return this.insuranceChargesField;
        }
        set {
            this.insuranceChargesField = value;
        }
    }

    /// <remarks/>
    public bool PartiesToTransactionAreRelated {
        get {
            return this.partiesToTransactionAreRelatedField;
        }
        set {
            this.partiesToTransactionAreRelatedField = value;
        }
    }

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

    /// <remarks/>
    public CommercialInvoice CommercialInvoice {
        get {
            return this.commercialInvoiceField;
        }
        set {
            this.commercialInvoiceField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("Commodities")]
    public Commodity[] Commodities {
        get {
            return this.commoditiesField;
        }
        set {
            this.commoditiesField = value;
        }
    }

    /// <remarks/>
    public ExportDetail ExportDetail {
        get {
            return this.exportDetailField;
        }
        set {
            this.exportDetailField = value;
        }
    }

    /// <remarks/>
    [System.Xml.Serialization.XmlElementAttribute("RegulatoryControls")]
    public RegulatoryControlType[] RegulatoryControls {
        get {
            return this.regulatoryControlsField;
        }
        set {
            this.regulatoryControlsField = value;
        }
    }
}





[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.3056.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://fedex.com/ws/rate/v24")]
public partial class Payment {

    private PaymentType paymentTypeField;

    private bool paymentTypeFieldSpecified;

    private Payor payorField;

    /// <remarks/>
    public PaymentType PaymentType {
        get {
            return this.paymentTypeField;
        }
        set {
            this.paymentTypeField = value;
        }
    }

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

    /// <remarks/>
    public Payor Payor {
        get {
            return this.payorField;
        }
        set {
            this.payorField = value;
        }
    }
}


[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.7.3056.0")]
[System.SerializableAttribute()]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://fedex.com/ws/rate/v24")]
public enum PaymentType {

    /// <remarks/>
    SENDER,
}

Solution

  • For these types, and some others, there was an extra property in place within the generated classes. For instance, if one of the properties that wasn't working was named "Property1", then there was another property named "Property1Specified". The serialization would ignore those properties unless the "...Specified" one was set to true. Setting all of those properties specified properties to true now allows the serialization to display all expected elements.

    As I said it was not about XML properties, or structure or smht like that. The reason was in setup of the XML and object to be serialized