c#:boolean values not getting converted when coverting from object to xml

Viewed 127

I am trying to convert object to xml. i have written a class which converts object to xml. in my object i have boolean and string variables. But when i convert only string variable a getting converted and not boolean. Please help me to resolve this issue. Below is my code.

LocateRequest stockLoanRequest = new LocateRequest();
stockLoanRequest.acceptManualApproval = false;
stockLoanRequest.acceptPartial = false;
stockLoanRequest.market = "US";
stockLoanRequest.cashOrSwap = cashOrSwapType.CASH;
stockLoanRequest.requestedQuantity = request.shares.ToString();
stockLoanRequest.requestorReference = request.locateId;
stockLoanRequest.securityId = request.symbol;
stockLoanRequest.securityIdentifierType = securityIdentifierType.TICKER;
return stockLoanRequest;
            

Convert object to xml code:

public static string convertRequestToXML(Object obj)
{
    XmlSerializer serializer = null;
     StringWriterUTF8 sw = new StringWriterUTF8();

     XmlTextWriter tw = null;
     try
     {
         serializer = new XmlSerializer(obj.GetType());
         tw = new XmlTextWriter(sw);
         tw.Formatting = Formatting.Indented;
         serializer.Serialize(tw, obj);
     }
     catch (Exception ex)
     {
         //Handle Exception Code
     }
     finally
     {
         sw.Close();
         if (tw != null)
         {
             tw.Close();
         }
     }

     return sw.ToString();
 }

Output to xml looks like below:

<LocateRequests>
    <LocateRequest securityId="IBM" securityIdentifierType="TICKER" market="US" requestedQuantity="1" requestorReference="34" />
</LocateRequests>

I don't see the boolean values acceptManualApproval and acceptPartial getting in the above output.

Below is the locate request class:

public partial class LocateRequest 
{        
    private bool acceptManualApprovalField;       
    private bool acceptManualApprovalFieldSpecified;       
    private bool acceptPartialField;       
    private bool acceptPartialFieldSpecified;      
    private string securityIdField;        
    private securityIdentifierType securityIdentifierTypeField;       
    private string marketField;        
    private string requestedQuantityField;        
    private string requestorReferenceField;        
    private cashOrSwapType cashOrSwapField;        
    private bool cashOrSwapFieldSpecified;

    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public bool acceptManualApproval {
        get {
            return this.acceptManualApprovalField;
        }
        set {
            this.acceptManualApprovalField = value;
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool acceptManualApprovalSpecified {
        get {
            return this.acceptManualApprovalFieldSpecified;
        }
        set {
            this.acceptManualApprovalFieldSpecified = value;
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public bool acceptPartial {
        get {
            return this.acceptPartialField;
        }
        set {
            this.acceptPartialField = value;
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool acceptPartialSpecified {
        get {
            return this.acceptPartialFieldSpecified;
        }
        set {
            this.acceptPartialFieldSpecified = value;
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute(DataType="token")]
    public string securityId {
        get {
            return this.securityIdField;
        }
        set {
            this.securityIdField = value;
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public securityIdentifierType securityIdentifierType {
        get {
            return this.securityIdentifierTypeField;
        }
        set {
            this.securityIdentifierTypeField = value;
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute(DataType="token")]
    public string market {
        get {
            return this.marketField;
        }
        set {
            this.marketField = value;
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute(DataType="positiveInteger")]
    public string requestedQuantity {
        get {
            return this.requestedQuantityField;
        }
        set {
            this.requestedQuantityField = value;
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute(DataType="token")]
    public string requestorReference {
        get {
            return this.requestorReferenceField;
        }
        set {
            this.requestorReferenceField = value;
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlAttributeAttribute()]
    public cashOrSwapType cashOrSwap {
        get {
            return this.cashOrSwapField;
        }
        set {
            this.cashOrSwapField = value;
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool cashOrSwapSpecified {
        get {
            return this.cashOrSwapFieldSpecified;
        }
        set {
            this.cashOrSwapFieldSpecified = value;
        }
    }
 }
1 Answers

There is a problem with boolean properties that suffixed by "Specified", it's ignore properties without the suffix. for example acceptPartialSpecified ignore acceptPartial in the xml during serialization.

To resolve the issue, rename all specified properties like SpecifiedPropertyName or delete them from the class.

Members with names ending on 'Specified' suffix have special meaning to the XmlSerializer: they control serialization of optional ValueType members and have to be of type System.Boolean.

I hope you find this helpful.

Related