NETCF平台下利用XmlSerializer对于复杂类型序列化的探索(三)
生活随笔
收集整理的這篇文章主要介紹了
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/archive/2006/12/15/593577.html
《新程序員》:云原生和全面數字化實踐50位技術專家共同創作,文字、視頻、音頻交互閱讀總結
以上是生活随笔為你收集整理的NETCF平台下利用XmlSerializer对于复杂类型序列化的探索(三)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 今天刚申请,呵呵
- 下一篇: 一款不错的基于WEB技术的文件服务器