7 years C/C++/C# programing, focus on embedded and mobile device development.

  博客园 :: 首页 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
  72 随笔 :: 1 文章 :: 132 评论 :: 5 引用

首先,来看一个简单的例子,其在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中正常工作,还需要很多额外的工作,后面等总结出来后再发上来。
posted on 2006-12-15 18:02 woaiusd 阅读(1723) 评论(1) 编辑 收藏

评论

出售蓝奇高级验证码识别引擎,可准确识别新浪动网淘宝CSDN等多种复杂验证码。

输出为一个标准DLL,可供VB,VC,Delphi,C#.NET,VB.NET,模拟精灵,按键精灵等多平台调用,调用方法简单,几行代码即可完成。独具特色的边缘检测字符分离、旋转倾斜纠正和通用字符匹配算法(无论字体和大小), 使得该引擎对于像新浪、动网、淘宝、CSDN等多种验证码均有不错的识别率,是一款效果较为理想的验证码识别引擎。附详细的调用实例和代码注释等相关技术文档。

官方网站 - http://***/yzm_advocr
识别效果怎么样一试就知道 - DEMO下载 http://***/yzm_advocr/advocr.rar
 回复 引用