简体   繁体   中英

XML serialization C#, not processing certain fields

I have a serialization method as follows:

public string SerializeObject(object obj, Type type)
{
    var setting = new XmlWriterSettings() { OmitXmlDeclaration = true, Indent = true };

    var xml = new StringBuilder();
    using (var writer = XmlWriter.Create(xml, setting))
    {
        var nsSerializer = new XmlSerializerNamespaces();
        nsSerializer.Add(string.Empty, string.Empty);

        var xmlSerializer = new XmlSerializer(type);
        xmlSerializer.Serialize(writer, obj, nsSerializer);
    }
    return xml.ToString();
}

Which I call as follows:

requestXML = serializer.SerializeObject(InboundIterate, typeof(INBOUND));

The output is as expected for any field I have defined in my structure as a string, but all the decimal values are missing.

For example my output will look like:

<PRODUCT_EXTENSION>
<DIMENSION_UOM>IN</SD_DIMENSION_UOM>
<SALES_UOM>CS</SD_SALES_UOM>
</PRODUCT_EXTENSION>

when I am expecting

<PRODUCT_EXTENSION>
<DIMENSION_UOM>IN</DIMENSION_UOM>
<DIMENSION>15.83</DIMENSION>
<SALES_UOM>CS</SALES_UOM>
<SALES>24</SALES>
</PRODUCT_EXTENSION>

any help would be appreciated, thank you.

class below

public partial class PRODUCT_EXTENSION {

    private System.Nullable<decimal> LENGTHField;

    private bool LENGTHFieldSpecified;

    private System.Nullable<decimal> NET_WEIGHTField;

    private bool NET_WEIGHTFieldSpecified;

    private string SALES_UOMField;

    private string WEIGHT_UOMField;

    private List<PRODUCT_EXTENSIONSOURCE_SYSTEM> SOURCE_SYSTEMField;

    public PRODUCT_EXTENSION() {
        this.SOURCE_SYSTEMField = new List<PRODUCT_EXTENSIONSOURCE_SYSTEM>();
    }

    public System.Nullable<decimal> LENGTH {
        get {
            return this.LENGTHField;
        }
        set {
            this.LENGTHField = value;
        }
    }

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

    public System.Nullable<decimal> NET_WEIGHT {
        get {
            return this.NET_WEIGHTField;
        }
        set {
            this.NET_WEIGHTField = value;
        }
    }

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

    public string SALES_UOM {
        get {
            return this.SALES_UOMField;
        }
        set {
            this.SALES_UOMField = value;
        }
    }

    public string SD_WEIGHT_UOM {
        get {
            return this.WEIGHT_UOMField;
        }
        set {
            this.WEIGHT_UOMField = value;
        }
    }

    public List<PRODUCT_EXTENSIONSOURCE_SYSTEM> SOURCE_SYSTEM {
        get {
            return this.SOURCE_SYSTEMField;
        }
        set {
            this.SOURCE_SYSTEMField = value;
        }
    }
}

I've tried your code snippet, and it works fine for me fine with public properties, but if i change the property protection to protected or private. These properties are missing from the XML.

Check your properties.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM