0

我有一个序列化方法如下:

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();
}

我称之为:

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

对于我在结构中定义为字符串的任何字段,输出都符合预期,但所有十进制值都丢失了。

例如,我的输出将如下所示:

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

当我期待

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

任何帮助将不胜感激,谢谢。

下面的课

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;
        }
    }
}
4

1 回答 1

2

我已经尝试了您的代码片段,它对我来说对公共属性很好,但是如果我将属性保护更改为受保护或私有。XML 中缺少这些属性。

检查您的属性。

于 2013-06-11T19:41:35.153 回答