0

This is from a WSDL document:

<s:complexType name="ResponseMsg">
<s:all>
<s:element minOccurs="0" maxOccurs="1" name="Responsestatus" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="CurBalance" type="s:double" />
<s:element minOccurs="0" maxOccurs="1" name="AvlBalance" type="s:double" />
<s:element minOccurs="1" maxOccurs="1" name="Acknowledgement" type="s:string" />
<s:element minOccurs="0" maxOccurs="1" name="LoadAmount" type="s:double" />
<s:element minOccurs="0" maxOccurs="1" name="Bill_Amt_Approved" type="s:double" />
<s:element minOccurs="0" maxOccurs="1" name="Update_Balance" type="s:int" />
<s:element minOccurs="0" maxOccurs="1" name="New_Balance_Sequence_Exthost" type="s:long" />
<s:element minOccurs="0" maxOccurs="1" name="CVV2_Result" type="s:string" /> <s:element minOccurs="0" maxOccurs="1" name="AvlBalance_GPS_STIP" type="s:double" /> <s:element minOccurs="0" maxOccurs="1" name="CurBalance_GPS_STIP" type="s:double" />
</s:all>
</s:complexType>

After adding the WSDL to my Visual Studio project (Add Service Reference), I get this:

/// <remarks/>
[System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.8.3752.0")]
[System.SerializableAttribute()]
[System.Diagnostics.DebuggerStepThroughAttribute()]
[System.ComponentModel.DesignerCategoryAttribute("code")]
[System.Xml.Serialization.XmlTypeAttribute(Namespace="http://tempuri.org/")]
public partial class ResponseMsg : object, System.ComponentModel.INotifyPropertyChanged {
    
    private string responsestatusField;
    
    private double curBalanceField;
    
    private bool curBalanceFieldSpecified;
    
    private double avlBalanceField;
    
    private bool avlBalanceFieldSpecified;
    
    private string acknowledgementField;
    
    private double loadAmountField;
    
    private bool loadAmountFieldSpecified;
    
    private double bill_Amt_ApprovedField;
    
    private bool bill_Amt_ApprovedFieldSpecified;
    
    private int update_BalanceField;
    
    private bool update_BalanceFieldSpecified;
    
    private long new_Balance_Sequence_ExthostField;
    
    private bool new_Balance_Sequence_ExthostFieldSpecified;
    
    private string cVV2_ResultField;
    
    private double avlBalance_GPS_STIPField;
    
    private bool avlBalance_GPS_STIPFieldSpecified;
    
    private double curBalance_GPS_STIPField;
    
    private bool curBalance_GPS_STIPFieldSpecified;
    
    /// <remarks/>
    public string Responsestatus {
        get {
            return this.responsestatusField;
        }
        set {
            this.responsestatusField = value;
            this.RaisePropertyChanged("Responsestatus");
        }
    }
    
    /// <remarks/>
    public double CurBalance {
        get {
            return this.curBalanceField;
        }
        set {
            this.curBalanceField = value;
            this.RaisePropertyChanged("CurBalance");
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool CurBalanceSpecified {
        get {
            return this.curBalanceFieldSpecified;
        }
        set {
            this.curBalanceFieldSpecified = value;
            this.RaisePropertyChanged("CurBalanceSpecified");
        }
    }
    
    /// <remarks/>
    public double AvlBalance {
        get {
            return this.avlBalanceField;
        }
        set {
            this.avlBalanceField = value;
            this.RaisePropertyChanged("AvlBalance");
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool AvlBalanceSpecified {
        get {
            return this.avlBalanceFieldSpecified;
        }
        set {
            this.avlBalanceFieldSpecified = value;
            this.RaisePropertyChanged("AvlBalanceSpecified");
        }
    }
    
    /// <remarks/>
    public string Acknowledgement {
        get {
            return this.acknowledgementField;
        }
        set {
            this.acknowledgementField = value;
            this.RaisePropertyChanged("Acknowledgement");
        }
    }
    
    /// <remarks/>
    public double LoadAmount {
        get {
            return this.loadAmountField;
        }
        set {
            this.loadAmountField = value;
            this.RaisePropertyChanged("LoadAmount");
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool LoadAmountSpecified {
        get {
            return this.loadAmountFieldSpecified;
        }
        set {
            this.loadAmountFieldSpecified = value;
            this.RaisePropertyChanged("LoadAmountSpecified");
        }
    }
    
    /// <remarks/>
    public double Bill_Amt_Approved {
        get {
            return this.bill_Amt_ApprovedField;
        }
        set {
            this.bill_Amt_ApprovedField = value;
            this.RaisePropertyChanged("Bill_Amt_Approved");
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool Bill_Amt_ApprovedSpecified {
        get {
            return this.bill_Amt_ApprovedFieldSpecified;
        }
        set {
            this.bill_Amt_ApprovedFieldSpecified = value;
            this.RaisePropertyChanged("Bill_Amt_ApprovedSpecified");
        }
    }
    
    /// <remarks/>
    public int Update_Balance {
        get {
            return this.update_BalanceField;
        }
        set {
            this.update_BalanceField = value;
            this.RaisePropertyChanged("Update_Balance");
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool Update_BalanceSpecified {
        get {
            return this.update_BalanceFieldSpecified;
        }
        set {
            this.update_BalanceFieldSpecified = value;
            this.RaisePropertyChanged("Update_BalanceSpecified");
        }
    }
    
    /// <remarks/>
    public long New_Balance_Sequence_Exthost {
        get {
            return this.new_Balance_Sequence_ExthostField;
        }
        set {
            this.new_Balance_Sequence_ExthostField = value;
            this.RaisePropertyChanged("New_Balance_Sequence_Exthost");
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool New_Balance_Sequence_ExthostSpecified {
        get {
            return this.new_Balance_Sequence_ExthostFieldSpecified;
        }
        set {
            this.new_Balance_Sequence_ExthostFieldSpecified = value;
            this.RaisePropertyChanged("New_Balance_Sequence_ExthostSpecified");
        }
    }
    
    /// <remarks/>
    public string CVV2_Result {
        get {
            return this.cVV2_ResultField;
        }
        set {
            this.cVV2_ResultField = value;
            this.RaisePropertyChanged("CVV2_Result");
        }
    }
    
    /// <remarks/>
    public double AvlBalance_GPS_STIP {
        get {
            return this.avlBalance_GPS_STIPField;
        }
        set {
            this.avlBalance_GPS_STIPField = value;
            this.RaisePropertyChanged("AvlBalance_GPS_STIP");
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool AvlBalance_GPS_STIPSpecified {
        get {
            return this.avlBalance_GPS_STIPFieldSpecified;
        }
        set {
            this.avlBalance_GPS_STIPFieldSpecified = value;
            this.RaisePropertyChanged("AvlBalance_GPS_STIPSpecified");
        }
    }
    
    /// <remarks/>
    public double CurBalance_GPS_STIP {
        get {
            return this.curBalance_GPS_STIPField;
        }
        set {
            this.curBalance_GPS_STIPField = value;
            this.RaisePropertyChanged("CurBalance_GPS_STIP");
        }
    }
    
    /// <remarks/>
    [System.Xml.Serialization.XmlIgnoreAttribute()]
    public bool CurBalance_GPS_STIPSpecified {
        get {
            return this.curBalance_GPS_STIPFieldSpecified;
        }
        set {
            this.curBalance_GPS_STIPFieldSpecified = value;
            this.RaisePropertyChanged("CurBalance_GPS_STIPSpecified");
        }
    }
    
    public event System.ComponentModel.PropertyChangedEventHandler PropertyChanged;
    
    protected void RaisePropertyChanged(string propertyName) {
        System.ComponentModel.PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
        if ((propertyChanged != null)) {
            propertyChanged(this, new System.ComponentModel.PropertyChangedEventArgs(propertyName));
        }
    }
}

I added this code to my project (found elsewhere):

public static string ObjectToXml<T>(this T obj) where T : class
{
    var settings = new XmlWriterSettings
    {
        Indent = true,
        OmitXmlDeclaration = false,
        Encoding = Encoding.GetEncoding("UTF-8")
    };

    var namespaces = new XmlSerializerNamespaces();
    namespaces.Add(string.Empty, string.Empty);

    var serializer = new XmlSerializer(obj.GetType());

    using (var stringWriter = new StringWriter())
    {
        using (var xmlWriter = XmlWriter.Create(stringWriter, settings))
        {
            serializer.Serialize(xmlWriter, obj, namespaces);
        }
        return stringWriter.ToString();
    }
}

And called it like this:

var rtnAck = new ResponseMsg
{
    Responsestatus = "00",
    CurBalance = 0.5,
    AvlBalance = 0.5,
    Acknowledgement = "1",
    LoadAmount = 0.00,
    CVV2_Result = "test"
};

var rtnData = rtnAck.ObjectToXml();

But the resulting string is

<?xml version="1.0" encoding="utf-16"?>
<ResponseMsg>
  <Responsestatus xmlns="http://tempuri.org/">00</Responsestatus>
  <Acknowledgement xmlns="http://tempuri.org/">1</Acknowledgement>
  <CVV2_Result xmlns="http://tempuri.org/">test</CVV2_Result>
</ResponseMsg>

Where are the other fields I set values for? The only ones to appear are of type string, the missing ones are double.

Also, how do I get rid of the xmlns on each element?

GordonS
  • 193
  • 4
  • 9
  • For the extra question, see here: https://stackoverflow.com/questions/258960/how-to-serialize-an-object-to-xml-without-getting-xmlns – Klaus Gütter Oct 30 '20 at 14:29
  • You have the following attribute which you can remove : System.Xml.Serialization.XmlIgnoreAttribute() – jdweng Oct 30 '20 at 14:40
  • @jdweng the XmlIgnoreAttribute is part of the code that was auto-generated by Visual Studio (via the Add - Service Reference option) – GordonS Oct 30 '20 at 14:47
  • So What!!! A Microsoft tools not doing what you want it to do. And trying to find the option to stop the unwanted results. I've spent months (maybe years) trying to stop Microsoft Tools from doing the wrong thing. – jdweng Oct 30 '20 at 15:00

1 Answers1

1

Do you see the properties ending with "Specified"? These are present so that the cases "attribute is present" and "attribute is missing" can be distinuished. For string properties, this is implicit by != null or == null.

So for non-string properties, you need to set the ...Specified properties to true if you want the value to be serialized:

var rtnAck = new ResponseMsg
{
    Responsestatus = "00",
    CurBalance = 0.5,
    CurBalanceSpecified = true,
    AvlBalance = 0.5,
    AvlBalanceSpecified = true, 
    Acknowledgement = "1",
    LoadAmount = 0.00,
    LoadAmountSpecified = true,
    CVV2_Result = "test"
};

This is not very user-friendly, but the XmlSerialization predates Nullable<T>.

Klaus Gütter
  • 11,151
  • 6
  • 31
  • 36