C # how to convert arraylist with complicated structure returned by WebService [original]

Source: Internet
Author: User

Let's take a look at my WebService method:

 1 [Webmethod]
2 Public Arraylist getouterlinkimage ( String Storeid)
3 {
4 Arraylist arr = New Arraylist ();
5 List < String > STRs = New List < String > ();
6 STRs. Add ( " AA " );
7 STRs. Add ( " Aaa " );
8 Arr. Add ( New Dictionaryentry ( " A " , STRs ));
9 Arr. Add ( New Dictionaryentry ( " B " , " Bb " ));
10 Arr. Add ( New Dictionaryentry ( " C " , " CC " ));
11
12 Return Arr;
13 }

This structure should be complicated: the following error will be reported after the call. After searching online, the returned structure cannot be converted to an XML structure.

System. invalidoperationexception: An error occurred while generating the XML document. ---> System. invalidoperationexception: the type of system. Collections. dictionaryentry should not be used. Use the xmlinclude or soapinclude feature to statically specify unknown types.

Next we will change the method to the following, and pay attention to the red font section (Here we add the types used in the method, each line is one, which means that there will be these types of nodes in XML at that time, I don't know if this statement is correct ):

 1 [Webmethod]
2 [Xmlinclude (typeof (arraylist)]
3[Xmlinclude (typeof (dictionaryentry)]
4[Xmlinclude (typeof (list <string>)]
5 Public Arraylist getouterlinkimage ( String Storeid)
6 {
7 Arraylist arr = New Arraylist ();
8 List < String > STRs =New List < String > ();
9 STRs. Add ( " AA " );
10 STRs. Add ( " Aaa " );
11 Arr. Add ( New Dictionaryentry ( " A " , STRs ));
12 Arr. Add ( New Dictionaryentry ( " B " , " Bb " ));
13 Arr. Add ( New Dictionaryentry ( " C " , " CC " ));
14
15 Return Arr;
16 }

Therefore, after three lines are added, no error is reported, and the XML structure is generated as follows:

xmlns: xsi = " http://www.w3.org/2001/XMLSchema-instance " xmlns: XSD = " http://www.w3.org/2001/XMLSchema " xmlns =" http://tempuri.org/ " <Anytype Xsi: Type="Dictionaryentry"> <Key Xsi: Type="XSD: String"> A </Key> <Value Xsi: Type="Arrayofstring"> <String> AA </String> <String> Aaa </String> </Value> </Anytype> <Anytype Xsi: Type="Dictionaryentry"> <Key Xsi: Type="XSD: String"> B </Key> <Value Xsi: Type="XSD: String"> Bb </Value> </Anytype> <Anytype Xsi: Type="Dictionaryentry"> <Key Xsi: Type="XSD: String"> C </Key> <Value Xsi: Type="XSD: String"> CC </Value> </Anytype> </Arrayofanytype>

This should be okay. Okay, let's call it on the client,CodeAs follows:

1Arraylist arrlist = WebService. getouterlinkimage (storeid );

Actually, an error is reported: The type "object []" cannot be implicitly converted to "system. collections. arraylist ", so you may try to convert it to dictionaryentry []. What is the result? Still, it still cannot be converted. What should I do? Can I convert it to object []? Isn't it equal to no conversion, however, we can obtain the following types:
Object [] objs = WebService. getouterlinkimage (storeid );
Type T = objs. GetType ();
Monitor what t is.

Isarray, by the way, it is an array, so: array arr = objs as array; conversion successful

So the following is the result of the call:

1Arraylist arrlist =NewArraylist ();
2Object[] Objs = WebService. getouterlinkimage (storeid );
3Type T = objs. GetType ();
4Array arr = objsAsArray;
5Foreach(VaRItemInARR)
6{
7Arrlist. Add (item );
8}

In this way, the object is converted.

So is the dictionaryentry type in arrlist one by one? Let's take a look,

Foreach (VAR item in arrlist)
{
// Debugging found that item is not system. collections. dictionaryentry is a WebService. dictionaryentry type. net reflection into a dictionaryentry class, although the structure is the same, there are key, value, but still can only be converted to WebService. dictionaryentry. For the reason, you may need to look at the WebService mechanism. I am also exploring it.
}

Now that you have obtained the dictionaryentry, you will know how to do it.
Over.

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.