C# 元素类型dt:dt命名空间的XmlSerializer属性命名空间

C# 元素类型dt:dt命名空间的XmlSerializer属性命名空间,c#,xml,xml-serialization,xml-namespaces,xmlserializer,C#,Xml,Xml Serialization,Xml Namespaces,Xmlserializer,我正在寻找XmlSerializer功能,以便在输出XML中重新创建一些名称空间/类型信息 因此,我必须像这样将XML输出复制到旧的COM应用程序: <Amount dt:dt="int">500</Amount> <StartTime dt:dt="dateTime">2014-12-30T12:00:00.000</StartTime> 我的XmlSerializer和名称空间设置如下: XmlSerializerNamesp

我正在寻找XmlSerializer功能,以便在输出XML中重新创建一些名称空间/类型信息

因此,我必须像这样将XML输出复制到旧的COM应用程序:

<Amount dt:dt="int">500</Amount>  
<StartTime dt:dt="dateTime">2014-12-30T12:00:00.000</StartTime>      
我的XmlSerializer和名称空间设置如下:

XmlSerializerNamespaces namespaces = new XmlSerializerNamespaces();
namespaces.Add("dt", "urn:schemas-microsoft-com:datatypes");
s.Serialize(writer, group, namespaces);
但这只会给我以下输出:

<dt:Amount>500</dt:Amount> 
500
有人知道我哪里出错了吗?

指定元素本身的名称空间,而不是元素的属性。这就是为什么您会看到
dt:
前缀。在这一点上,我并没有帮助你;它用于指定多态字段的类型,不会自动生成
dt
。事实上,
XmlSerializer
中没有内置功能来自动生成属于名称空间的

因此,有必要使用具有必要属性的包装器结构手动执行此操作。以下实现是类型化的,而不是多态的:

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结构XdrTypeWrapper
{
类XdrTypeWrapperTypeDictionary
{
静态XdrTypeWrapperTypeDictionary实例;
静态XdrTypeWrapperTypeDictionary(){instance=new XdrTypeWrapper.XdrTypeWrapperTypeDictionary();}
公共静态XdrTypeWrapperTypeDictionary实例{get{return Instance;}}
只读词典;
XdrTypeWrapperTypeDictionary()
{
//取自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=新词典
{
{typeof(string),“string”},
{typeof(sbyte),“i1”},
{typeof(byte),“u1”},
{typeof(short),“i2”},
{typeof(ushort),“u2”},
{typeof(int),“int”},//本可以使用i4
{typeof(uint),“ui4”},
{typeof(long),“i8”},
{typeof(ulong),“ui8”},
{typeof(DateTime),“DateTime”},
{typeof(bool),“boolean”},
{typeof(double),“float”},//本可以使用r8
{typeof(float),“r4”},
};
}
公共字符串数据类型(类型)
{
返回dict[type];
}
}
公共XdrTypeWrapper(T值){this.value=value;}
公共静态隐式运算符XdrTypeWrapper(T值){返回新的XdrTypeWrapper(值);}
公共静态隐式运算符T(XdrTypeWrapper){return wrapper.Value;}
[XmlAttribute(“dt”,Namespace=“urn:schemas-microsoft-com:datatypes”)]
公共字符串数据类型
{
得到
{
返回XdrTypeWrapperTypeDictionary.Instance.DataType(typeof(T));
}
设置
{
//什么也不做。
}
}
T值;
[XmlText]
公共T值{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; } }
}
公共类TestClass
{
[XmlIgnore]
公共整数金额{get;set;}
[XmlElement(“金额”)]
[可浏览(false)、可编辑(EditorBrowsableState.Never)、可调试(DebuggerBrowsableState.Never)]
公共XdrTypeWrapper XmlAmount{get{return Amount;}set{Amount=value;}}
[XmlIgnore]
公共日期时间开始时间{get;set;}
[XmlElement(“StartTime”)]
[可浏览(false)、可编辑(EditorBrowsableState.Never)、可调试(DebuggerBrowsableState.Never)]
公共XdrTypeWrapper XmlStartTime{get{return StartTime;}set{StartTime=value;}}
[XmlIgnore]
公共双精度{get;set;}
[XmlElement(“双”)]
[可浏览(false)、可编辑(EditorBrowsableState.Never)、可调试(DebuggerBrowsableState.Never)]
公共XdrTypeWrapper XmlDouble{get{return Double;}set{Double=value;}}
}
它生成以下XML:



.

序列化不会一直在复杂名称空间中工作。哇。一个非常深思熟虑和完整的答案,非常感谢dbc!我当然更了解XmlSerialize和数据类型之间的关系。小提琴的例子是锦上添花!
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; } }
}
<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>