Summary of WebService soap transmission serialization

Source: Internet
Author: User

1. All objects passed in WebService must be serializable. This is a necessary condition for transmission between networks. The XML WebService and soap standards support the following data types:

1. Basic data type.

Standard type, such as int float bool datetime string and other basic data types

2. enumeration.

Enum-defined types supported

3. Custom object.

You can pass any object created based on a custom class or structure. Note that only data members (variables and attributes) can be transmitted ).

If a method is defined, the method cannot be serialized and transmitted. Only data members are left after serialization.

4. DataSet object

Dataset is supported. Remember: datatable and datarow are not supported. dataset is already the smallest serializable object supported by XML WebService.

5. xmlnode object

Xmlnode-based objects can represent part of an XML document.

6. arrays and collections

Any supported types of arrays and simple collections can be used, including DataSet object/xmlnode object and custom object.

 

I don't know if the reader has encountered this situation. When calling WebService and passing a datarow parameter to a method, an exception will be thrown during runtime: "unable to serialize parameters! ", If you add datarow to dataset, and pass the dataset as a parameter before running it, it will be OK. This is because: XML WebService can only serialize the dataset DataSet object type in XML format, and Cannot serialize the datarow object type in XML format. therefore, it is important to understand the basic data types that XML WebService supports serialization.

Apart from the above basic types, the WebService Service cannot complete direct serialization and transmission, but most data structures can still be transmitted on the WebService with serialization.

For example:

When a custom class is serialized to a file, the following error message is displayed:

System. reflection. targetinvocationexception: An exception occurred to the calling target. --->

System. invalidoperationexception: An error occurred while generating the XML document. ---> System. invalidoperationexception:

It should not be of the alink. t1system. Windows. printsetting type. Use the xmlinclude or soapinclude attribute to statically specify unknown types.

 

After searching for information, we found that when the sorted class contains a Custom Complex class, we need to use the xmlincludeattribut attribute to identify the custom class.

The modification is as follows:

Using system. xml. serialization;

[Xmlinclude (typeof (custom class)]

[Serializable]

Public class mydiyclass

All custom classes are marked, and the WebService method provided is also marked.

[Webmethod]

[Xmlinclude (typeof (custom class)]

Public object webservicemethod (Object requestobj)

You can solve the problem by using xmlinclude once for the custom classes contained in the class;

In this way, the list <> and arraylist objects can be transmitted, but the [webmethod] method of the arraylist type is returned. After the client calls the method, the object array is obtained, in addition, all the properties in the object are changed to field. In addition, the interface cannot be serialized, for example, list <interface>. In this case, you can change the interface to an abstract class and add the [xmlinclude] attribute to the abstract class, note that the class you write must have a default constructor (a constructor without parameters) for serialization ).

 

XML serialization Q &:

(1) The field to be serialized must be public)

(2) All classes to be serialized must have a constructor without parameters.

(3) Enumeration variables can be serialized as strings without the use of [xmlinclude]

(4) to export non-basic type objects, you must use [xmlinclude] to declare them in advance. This rule recursively applies to child elements.

For example, to export an arraylist object, if its members are user-defined, pre-include processing is required:

Using system. xml. serialization;

[Xmlinclude (typeof (custom class)]

(5) If the isnullable parameter in attribute is false, it indicates that if the element is null, this element is not displayed.

That is to say, this function is effective for value types (such as struct ).

If the array contains 100 spaces and 10 class objects are filled, only 10 nodes are displayed after serialization.

If the array contains 100 space and 10 struct objects are filled, 100 nodes are displayed after serialization.

(6) If XML serialization is not possible, some classes cannot be serialized in XML (even if [xmlinclude] is used).

Idictionary (such as hashtable)

System. Drawing. Color

System. Drawing. Font

Securityattribute Declaration

When a parent class object is assigned a subclass object Value

Circular reference between objects

(7) For objects that cannot be serialized in XML, you can consider using custom XML serialization (implementing the ixmlserializable Interface) to implement idictionary classes. Consider

(1) Replace it with other collection classes; (2) encapsulate it with classes and provide the ADD and this Functions

Some types must be converted before being serialized into XML. For example, in XML serialization of system. Drawing. color, you can first convert toargb () to an integer. If the XML serialization of an object is too complex, binary serialization can be considered.

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.