Using xmlserializer on the netcf platform to explore the serialization of complex types (3)

Source: Internet
Author: User

First, let's look at a simple example where both PC and PDA can be smoothly serialized and deserialized.

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;
}
}

In a specific test Code The serialized XML file is as follows: <? XML version = "1.0" ?>
< NS: triggercommand Xmlns: NS = "Urn: epcglobal: RP: XSD: 1" >
< Name > Hello </ Name >
< Sethandle > 123.456 </ Sethandle >
</ NS: triggercommand >

The members in this class actually correspond to the choice in the XSD. When the options in the choice have different types and different names (in the above example), xmlserialize automatically recognizes the data type, only one object field is required,ProgramYou can forcibly convert the type of an item.

The following code processes any in XSD based on our scalability and compatibility requirements. The modified code is as follows: [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;
}
}

The same XML document can be output during the test under the PC, but the following exception is returned when the xmlserializer is created during the test on the PDA:
You need to add xmlchoiceidentifierattribute To The 'item' member.

It may be that the xmlelement and other types cannot be distinguished Under netcf, which breaks the constraint that "when the options in choice have different types and different names, therefore, the User-Defined enumeration class should be added with the following code: 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. Now the same XML output can be obtained on both the PC and PDA.

At this point, the problems encountered by serialization of complex objects in netcf are basically solved. Of course, it is necessary to handle the complex XSD in EPC and make it work normally in netcf, a lot of extra work is required, and we will try again later.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.