C#如何把XSD中HexBinary类型序列化uint类型

xml schema中有hexBinary类型, 我们在实现C#的Serialization时,一般默认把hexBinary映射为byte[],但是有些情况我们需要把 hexBinary映射为uint、int等等这样的类型。 这样我们就需要包装下, 如下:

xml schema中定义ID节点, 类型为hexBinary。我们通过中间byte[] IDBinary转为uint ID, 实际使用中直接使用ID即可。


               <xs:element name="ID">
                    <xs:simpleType>
                         <xs:restriction base="xs:hexBinary">
                              <xs:length value="4"/>
                         </xs:restriction>
                    </xs:simpleType>
               </xs:element>

        [XmlIgnore()]
        public uint ID
        {
            get;
            set;
        }

        [XmlElement("ID", DataType = "hexBinary")]
        public byte[] IDBinary
        {
            get
            {
                return BitConverter.GetBytes(ID).Reverse().ToArray();
            }
            set
            {
                ID = BitConverter.ToUInt32(value.Reverse().ToArray(), 0);
            }
        }
posted @ 2013-07-03 16:48  muzizongheng  阅读(452)  评论(0编辑  收藏  举报
如果我们时时忙着展现自己的知识, 将何从忆起成长所需的无知?