"Go" Java access to the data set returned by. NET WebService

Source: Internet
Author: User

Turn from "Turn is also turn" "http://blog.csdn.net/fox123871/article/details/8637839"

1. Overview
  
Many programmers who are developing or planning to develop XML WEB services have asked such a question: "My webservice returns the result of a DataSet type object, but if my client is not used." NET write (and thus no built-in dataset type), how do I call this webservice and access the data in the dataset? "。
  
The first thing to say about this question is: 1) in a programming environment where multiple languages coexist, it is not appropriate to use a dataset-like data type that is only specific to a particular language. In both xmlwebservices and CORBA environments, you should try to use simple data types as well as arrays of simple data types. 2) It should be prudent to decide whether to return large amounts of data through webservice. Because the overhead of network transport includes both the time the HTTP connection is established and the time it takes to transfer the data, it is necessary to find a suitable balance between reducing the number of access servers and reducing the amount of network traffic. If not required, it is not appropriate to transmit data tables containing dozens of or hundreds of data via webservice.
  
Then, as far as the problem itself is concerned, the dataset type returned by. NET WebServices can be parsed directly by other non-. NET clients, because even the return value of the dataset type is expressed in XML format for transmission. The following example is a WebMethod with a return type of dataset and the XML format data returned after it is called:
  
Table 1. Returns a web Method of type dataset

1 [WebMethod]2      PublicDataSet getpersondata ()3 {4DataTable table=NewDataTable (" Person");5Table. Columns.Add ("Name");6Table. Columns.Add ("Gender");7Table. Rows.Add (New string[2]{"Alice","Female"});8Table. Rows.Add (New string[2]{"Bob","Male"});9Table. Rows.Add (New string[2]{"Chris","Male"});Ten      OneDataSet dataset=NewDataSet ("persontable"); A DataSet. Tables.add (table); -     returnDataSet; -}

Table 2. Datasets that are formatted as XML

1<?xml version="1.0"encoding="Utf-8"?>2<datasetxmlns="http://tempuri.org/">3<xs:schema id="persontable"xmlns=""xmlns:xs="Http://www.w3.org/2001/XMLSchema"4Xmlns:msdata="Urn:schemas-microsoft-com:xml-msdata">5<xs:element name="persontable"msdata:isdataset="true"msdata:locale="ZH-CN">6<xs:complexType>7<xs:choicemaxoccurs="unbounded">8<xs:element name=" Person">9<xs:complexType>Ten<xs:sequence> One<xs:element name="Name"Type="xs:string"minoccurs="0"/> A<xs:element name="Gender"Type="xs:string"minoccurs="0"/> -</xs:sequence> -</xs:complexType> the</xs:element> -</xs:choice> -</xs:complexType> -</xs:element> +</xs:schema> -<diffgr:diffgramxmlns:msdata="Urn:schemas-microsoft-com:xml-msdata" +Xmlns:diffgr="Urn:schemas-microsoft-com:xml-diffgram-v1"> A<persontable xmlns=""> at<person diffgr:id="Person1"Msdata:roworder="0"diffgr:haschanges="inserted"> -<Name>Alice</Name> -<Gender>Female</Gender> -</Person> -<person diffgr:id="Person2"Msdata:roworder="1"diffgr:haschanges="inserted"> -<Name>Bob</Name> in<Gender>Male</Gender> -</Person> to<person diffgr:id="Person3"Msdata:roworder="2"diffgr:haschanges="inserted"> +<Name>Chris</Name> -<Gender>Male</Gender> the</Person> *</PersonTable> $</diffgr:diffgram>Panax Notoginseng</DataSet>

As you can see from the example above, the result of using the dataset directly as a return type is quite complex, which includes not only the data in the dataset, but also information about the data changes and the schema of the dataset. While some tools can generate a dataset-like client type, either parsing complex XML directly or using a dataset-like class is not straightforward enough.

There are two ways to solve this problem:
  
1) Constructs a custom type with a simple data type, encapsulates a row in the dataset with each custom type object, returns an array of custom type objects (array) to the client, and the client is able to restore the definition of the custom type completely, with a simple data type definition;
  
2) Use the DataSet.WriteXml () method to extract the data from the dataset into an XML format and return it to the client as a string, and the client parses the XML string to restore the data. Because of the ability to filter out redundant information when using WriteXml (), the content returned is significantly simplified compared to the content in Figure 2.
  
2. Create a. NET Web Services, return a data collection
  
With Visual Studio.NET, you can create a practical webservices very quickly by simply writing the code of the Web method itself:
  
Table 3. XML Web Services implemented with. Net

1 [WebMethod]2      Publicperson[] Getpersons ()3 {4Person Alice=newperson ("Alice","Female");5Person Bob=newperson ("Bob","Male");6Person Chris=newperson ("Chris","Female");7Person Dennis=newperson ("Dennis","Male");8     9        returnNewperson[]{alice,bob,chris,dennis};Ten } One      A [WebMethod] -      Public stringgetpersontable () - { thedatatabletable=NewDataTable (" Person"); -Table. Columns.Add ("Name"); -Table. Columns.Add ("Gender"); -Table. Rows.Add (New string[2]{"Alice","Female"}); +Table. Rows.Add (New string[2]{"Bob","Male"}); -Table. Rows.Add (New string[2]{"Chris","Female"}); +Table. Rows.Add (New string[2]{"Dennis","Male"}); ATable. Rows.Add (newstring[2]{"Eric","Male"}); at      -datasetdataset=NewDataSet ("persontable"); - DataSet. Tables.add (table); -      -System.text.stringbuilderstrbuilder=NewSystem.Text.StringBuilder (); -StringWriter writer=NewStringWriter (strbuilder); in DataSet. WRITEXML (Writer,system.data.xmlwritemode.ignoreschema); -      to Returnstrbuilder. ToString (); +}

In the above code, the functions Getpersons () and getpersontable () correspond to the two solutions mentioned in "1. Overview", respectively. Where the person type is the custom data type used to encapsulate a row of data in the dataset:
  
Table 4. Custom type Person

1 [Serializable]2      Public class Person3 {4         PublicPerson ()5 {6 }7     8         PublicPerson (Stringname,stringgender)9 {Ten           This. Name=name; One           This. Gender=gender; A } -      -         Public stringName=""; the         PublicStringgender=""; -}

"Go" Java access to the data set returned by. NET WebService

Related Article

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.