我要序列化的类是由XSD.EXE根据XSD文件自动生成的,由于结构过于复杂,为说明问题这里进行了简化,现定义如下:
namespace
RFID.ReaderProxy
{
[XmlTypeAttribute(Namespace = "urn:epcglobal:rp:xsd:1")]
[XmlRootAttribute(Namespace = "urn:epcglobal:rp:xsd:1", IsNullable = false)]
public partial class TriggerCommand
{
[XmlElementAttribute(Form = XmlSchemaForm.Unqualified)]
public string name;
[XmlElementAttribute("create", typeof(int), Form = XmlSchemaForm.Unqualified)]
[XmlElementAttribute("fire", typeof(int), Form = XmlSchemaForm.Unqualified)]
[XmlChoiceIdentifierAttribute("ItemElementName")]
public int Item;
[XmlIgnoreAttribute()]
public TriggerCommandItemChoiceType ItemElementName;
}
[XmlTypeAttribute(Namespace = "urn:epcglobal:rp:xsd:1", IncludeInSchema = false)]
public enum TriggerCommandItemChoiceType
{
[XmlEnumAttribute(Name = ":create")]
create,
[XmlEnumAttribute(Name = ":fire")]
fire
}
}
上述的定义遵循了自动生成的代码中的大部分规则,和标准生成的代码最接近,但问题也最多。注意其中的Form = XmlSchemaForm.Unqualified和XmlEnumAttribute(Name = ":create"),后面我将会重点对此代码进行修改以查看效果。首先我对此代码进行测试,结果如下:
为PC平台创建的测试程序可以将对象序列化为如下XML文档:
<?
xml version
=
"
1.0
"
?>
<
TriggerCommand xmlns:xsi
=
"
http://www.w3.org/2001/XMLSchema-instance
"
xmlns:xsd
=
"
http://www.w3.org/2001/XMLSchema
"
xmlns
=
"
urn:epcglobal:rp:xsd:1
"
>
<
create xmlns
=
""
>
123
</
create
>
</
TriggerCommand
>
而为PDA平台创建侧测试程序在创建XmlSerializer对象时抛出异常:
Type RFID.ReaderProxy.TriggerCommandItemChoiceType is missing enumeration value 'fire' for element 'fire' from namespace ''.
下面尝试性的将代码中的Unqualified改为Qualified进行测试。
代码如下:
namespace
RFID.ReaderProxy
{
[XmlTypeAttribute(Namespace = "urn:epcglobal:rp:xsd:1")]
[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("fire", typeof(int), Form = XmlSchemaForm.Qualified)]
[XmlChoiceIdentifierAttribute("ItemElementName")]
public int Item;
[XmlIgnoreAttribute()]
public TriggerCommandItemChoiceType ItemElementName;
}
[XmlTypeAttribute(Namespace = "urn:epcglobal:rp:xsd:1", IncludeInSchema = false)]
public enum TriggerCommandItemChoiceType
{
[XmlEnumAttribute(Name = ":create")]
create,
[XmlEnumAttribute(Name = ":fire")]
fire
}
}
为PC平台创建侧测试程序在创建XmlSerializer对象时抛出异常:
Type RFID.ReaderProxy.TriggerCommandItemChoiceType is missing enumeration value 'urn:epcglobal:rp:xsd:1:create' for element 'create' from namespace 'urn:epcglobal:rp:xsd:1'.
为PDA平台创建的测试程序可以将对象序列化为如下XML文档:
<?
xml version="1.0"
?>
<
TriggerCommand
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd
="http://www.w3.org/2001/XMLSchema"
xmlns
="urn:epcglobal:rp:xsd:1"
>
<
create
>
0
</
create
>
</
TriggerCommand
>
下面将枚举类型TriggerCommandItemChoiceType中的[XmlEnumAttribute(Name = ":create")]中的冒号去掉,进行测试,说实话我也不知道这里这个冒号有什么作用。代码如下:
namespace
RFID.ReaderProxy
{
[XmlTypeAttribute(Namespace = "urn:epcglobal:rp:xsd:1")]
[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("fire", typeof(int), Form = XmlSchemaForm.Qualified)]
[XmlChoiceIdentifierAttribute("ItemElementName")]
public int Item;
[XmlIgnoreAttribute()]
public TriggerCommandItemChoiceType ItemElementName;
}
[XmlTypeAttribute(Namespace = "urn:epcglobal:rp:xsd:1", IncludeInSchema = false)]
public enum TriggerCommandItemChoiceType
{
[XmlEnumAttribute(Name = "create")]
create,
[XmlEnumAttribute(Name = "fire")]
fire
}
}
奇怪,这下全部OK了,在PC和PDA上面运行测试程序,都可以生成如下的XML文档:
<?
xml version="1.0"
?>
<
TriggerCommand
xmlns:xsi
="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd
="http://www.w3.org/2001/XMLSchema"
xmlns
="urn:epcglobal:rp:xsd:1"
>
<
create
>
123
</
create
>
</
TriggerCommand
>
而上述的XML文档正是我期望得到的文档样式。虽然目的达到了,但我始终想不通其中的道理,主要有下述几点:
1. XSD文档中定义的也是Unqualified, 默认生成的代码中也是应用的Unqualified,可这个Unqualified在PDA下面却无论如何也调试不通。
2. 枚举类型中的冒号表示什么意思?
3. 默认的Unqualified在PC上生成的XML文档为何在元素中带了一个命名空间的属性,如<create xmlns="">123</create>
4. 我最终修改的结果虽然可以生成期望的XML文档,但这是源于巧合或者是确实找到了正确了方法呢?
关于NETCF中复杂类型序列化的问题我会继续跟进,因为还有另外一个XmlAnyElementAttribute的问题,我会在下篇文章中提出。