NETCF平台下利用XmlSerializer对于复杂类型序列化的探索(三)

首先,来看一个简单的例子,其在PC和PDA上均可以顺利的序列化和反序列化。

namespace RFID.ReaderProxy
{
    [System.Xml.Serialization.XmlTypeAttribute(Namespace 
= "")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace 
= "urn:epcglobal:rp:xsd:1", IsNullable = false)]
    
public partial class TriggerCommand
    
{
        [XmlElementAttribute(Form 
= XmlSchemaForm.Qualified)]
        
public string name;

        [XmlElementAttribute(
"create"typeof(int), Form = XmlSchemaForm.Qualified)]
        [XmlElementAttribute(
"setHandle"typeof(double), Form = XmlSchemaForm.Qualified)]
        [XmlElementAttribute(
"fire"typeof(string), Form = XmlSchemaForm.Qualified)]
        
public object Item;
    }

}

在特定测试代码下其序列化后的XML文档如下
<?xml version="1.0"?>
<ns:TriggerCommand xmlns:ns="urn:epcglobal:rp:xsd:1">
  
<name>hello</name>
  
<setHandle>123.456</setHandle>
</ns:TriggerCommand>

这个类中的成员实际上对应这XSD中的choice,当choice中的选项具有不同的类型和不同的名称时(如上例),XmlSerialize会自动识别数据的类型,仅需一个object类型的字段即可,程序可以强制对item进行类型转换。

下面根据我们的扩展性和兼容性需要,处理XSD中的any, 修改后的代码如下:其中添加了[XmlAnyElementAttribute()]
namespace RFID.ReaderProxy
{
    [System.Xml.Serialization.XmlTypeAttribute(Namespace 
= "")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace 
= "urn:epcglobal:rp:xsd:1", IsNullable = false)]
    
public partial class TriggerCommand
    
{
        [XmlElementAttribute(Form 
= XmlSchemaForm.Qualified)]
        
public string name;

        [XmlAnyElementAttribute()]
        [XmlElementAttribute(
"create"typeof(int), Form = XmlSchemaForm.Qualified)]
        [XmlElementAttribute(
"setHandle"typeof(double), Form = XmlSchemaForm.Qualified)]
        [XmlElementAttribute(
"fire"typeof(string), Form = XmlSchemaForm.Qualified)]
        
public object Item;
    }

}

在PC下面测试时,可以输出相同的XML文档,但在PDA上测试时,创建XmlSerializer的时候得到如下的异常:
You need to add XmlChoiceIdentifierAttribute to the 'Item' member.

可能是在NETCF下面无法区分XmlElement和其它的类型吧,从而破坏了“当choice中的选项具有不同的类型和不同的名称时”约束,因此尝试性的加入自定义枚举类,代码如下:
namespace RFID.ReaderProxy
{
    [System.Xml.Serialization.XmlTypeAttribute(Namespace 
= "")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace 
= "urn:epcglobal:rp:xsd:1", IsNullable = false)]
    
public partial class TriggerCommand
    
{
        [XmlElementAttribute(Form 
= XmlSchemaForm.Qualified)]
        
public string name;

        [XmlAnyElementAttribute()]
        [XmlElementAttribute(
"create"typeof(int), Form = XmlSchemaForm.Qualified)]
        [XmlElementAttribute(
"setHandle"typeof(double), Form = XmlSchemaForm.Qualified)]
        [XmlElementAttribute(
"fire"typeof(string), Form = XmlSchemaForm.Qualified)]
        [XmlChoiceIdentifierAttribute(
"ItemElementName")]
        
public object Item;

        [XmlIgnoreAttribute()]
        
public TriggerCommandItemChoiceType ItemElementName;
    }


    [System.Xml.Serialization.XmlTypeAttribute(Namespace 
= "")]
    
public enum TriggerCommandItemChoiceType
    
{
        [System.Xml.Serialization.XmlEnumAttribute(
"##any:")]
        Item,

        [XmlEnumAttribute(Name 
= "create")]
        create,

        [XmlEnumAttribute(Name 
= "setHandle")]
        setHandle,

        [XmlEnumAttribute(Name 
= "fire")]
        fire
    }

}

OK,现在PC和PDA上均可以获得相同的XML输出了。

至此,复杂对象在NETCF中的序列化所遇到的问题基本上解决了,当然,要处理EPC中复杂的XSD并让其在NETCF中正常工作,还需要很多额外的工作,后面等总结出来后再发上来。
原文地址:https://www.cnblogs.com/swnuwangyun/p/593577.html