簡體   English   中英

元素類型dt:dt命名空間的XmlSerializer屬性命名空間

[英]XmlSerializer attribute namespace for element type dt:dt namespace

我正在尋找XmlSerializer功能以在輸出XML中重新創建一些名稱空間/類型信息。

因此,我必須將這樣的XML輸出復制到舊的COM應用程序中:

<Amount dt:dt="int">500</Amount>  
<StartTime dt:dt="dateTime">2014-12-30T12:00:00.000</StartTime>      

我目前以如下方式設置屬性的屬性:

[XmlElement(ElementName = "Amount", Namespace = "urn:schemas-microsoft-com:datatypes",  
DataType = "int", Type = typeof(int))]  
public int Amount{ get; set; }  

將我的XmlSerializer和命名空間設置如下:

XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add("dt", "urn:schemas-microsoft-com:datatypes");
s.Serialize(writer, group, namespaces);

但這只給我輸出:

<dt:Amount>500</dt:Amount> 

有人知道我要去哪里了嗎?

XmlElementAttribute.Namespace指定元素本身的名稱空間,而不是元素的屬性。 這就是為什么您看到dt:前綴的原因。 而且DataType = "int"在這里沒有幫助您; 它用於指定多態字段的類型,並且不會自動生成dt 數據類型attribute 實際上, XmlSerializer沒有內置功能來自動生成XDR數據類型屬性值 ,這些屬於命名空間"urn:schemas-microsoft-com:datatypes"

因此,有必要使用具有必要屬性的包裝器結構手動進行操作。 以下實現是類型化的,而不是多態的:

public struct XdrTypeWrapper<T>
{
    class XdrTypeWrapperTypeDictionary
    {
        static XdrTypeWrapperTypeDictionary instance;

        static XdrTypeWrapperTypeDictionary() { instance = new XdrTypeWrapper<T>.XdrTypeWrapperTypeDictionary(); }

        public static XdrTypeWrapperTypeDictionary Instance { get { return instance; } }

        readonly Dictionary<Type, string> dict;

        XdrTypeWrapperTypeDictionary()
        {
            // Taken from https://msdn.microsoft.com/en-us/library/ms256121.aspx
            // https://msdn.microsoft.com/en-us/library/ms256049.aspx
            // https://msdn.microsoft.com/en-us/library/ms256088.aspx
            dict = new Dictionary<Type, string>
            {
                { typeof(string), "string" },
                { typeof(sbyte), "i1" },
                { typeof(byte), "u1" },
                { typeof(short), "i2" },
                { typeof(ushort), "u2" },
                { typeof(int), "int" }, // Could have used i4
                { typeof(uint), "ui4" },
                { typeof(long), "i8" },
                { typeof(ulong), "ui8" },
                { typeof(DateTime), "dateTime" },
                { typeof(bool), "boolean" },
                { typeof(double), "float" }, // Could have used r8
                { typeof(float), "r4" },
            };
        }

        public string DataType(Type type)
        {
            return dict[type];
        }
    }

    public XdrTypeWrapper(T value) { this.value = value; }

    public static implicit operator XdrTypeWrapper<T>(T value) { return new XdrTypeWrapper<T>(value); }

    public static implicit operator T(XdrTypeWrapper<T> wrapper) { return wrapper.Value; }

    [XmlAttribute("dt", Namespace = "urn:schemas-microsoft-com:datatypes")]
    public string DataType
    {
        get
        {
            return XdrTypeWrapperTypeDictionary.Instance.DataType(typeof(T));
        }
        set
        {
            // Do nothing.
        }
    }

    T value;

    [XmlText]
    public T Value { get { return value; } set { this.value = value; } }
}

然后在具有代理屬性的類中使用它,如下所示:

public class TestClass
{
    [XmlIgnore]
    public int Amount { get; set; }

    [XmlElement("Amount")]
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DebuggerBrowsable(DebuggerBrowsableState.Never)]
    public XdrTypeWrapper<int> XmlAmount { get { return Amount; } set { Amount = value; } }

    [XmlIgnore]
    public DateTime StartTime { get; set; }

    [XmlElement("StartTime")]
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DebuggerBrowsable(DebuggerBrowsableState.Never)]
    public XdrTypeWrapper<DateTime> XmlStartTime { get { return StartTime; } set { StartTime = value; } }

    [XmlIgnore]
    public double Double { get; set; }

    [XmlElement("Double")]
    [Browsable(false), EditorBrowsable(EditorBrowsableState.Never), DebuggerBrowsable(DebuggerBrowsableState.Never)]
    public XdrTypeWrapper<double> XmlDouble { get { return Double; } set { Double = value; } }
}

產生以下XML:

 <TestClass xmlns:dt="urn:schemas-microsoft-com:datatypes"> <Amount dt:dt="int">101</Amount> <StartTime dt:dt="dateTime">2015-10-06T20:35:18.2308848+00:00</StartTime> <Double dt:dt="float">101.23</Double> </TestClass> 

原型擺弄

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM