Call. Net XML Web Services to return data integration

Source: Internet
Author: User
Tags net xml

1. Overview
  
Many programmers who are developing or planning to develop XML Web Services have asked the following question: "The result returned by my web service is a DataSet object, but if my client is not used. net write (so there is no built-in dataset type), then how to call this web service and access data in dataset? ".
  
To solve this problem, we should first say: 1) In a programming environment where multiple languages coexist, it is not suitable for Data Types similar to dataset that only belong to a specific language. Both XML Web Services and CORBA environments should use arrays of simple data types and simple data types as much as possible. 2) You should carefully decide whether to return a large amount of data through the web service. Because the overhead of network transmission includes both the time when an HTTP connection is established and the time when data is transmitted, you need to find an appropriate balance between reducing the number of access servers and reducing the amount of network transmission. If not required, it is not suitable for transmitting data tables containing dozens or hundreds of data records through Web Services.
  
Then, as far as the problem is concerned ,.. NET web services can be directly returned by other non. net client resolution, because even the dataset type return value, will be reached by the table XML format and then transmitted. The following example shows a web method with the dataset type returned and the XML format data returned after being called:
  
Table 1. web method whose return type is Dataset
[Webmethod]
Public dataset getpersondata ()
{
Datatable table = new datatable ("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", "male "});
  
Dataset dataset = new dataset ("persontable ");
Dataset. Tables. Add (table );
Return dataset;
}
  
Table 2. dataset formatted as XML
<? XML version = "1.0" encoding = "UTF-8"?>
<Dataset xmlns = "http://tempuri.org/">
<Xs: schema id = "persontable" xmlns = "" xmlns: xs = "http://www.w3.org/2001/XMLSchema"
Xmlns: msdata = "urn: Schemas-Microsoft-com: XML-msdata">
<Xs: element name = "persontable" msdata: isdataset = "true" msdata: locale = "ZH-CN">
<Xs: complextype>
<Xs: Choice maxoccurs = "unbounded">
<Xs: element name = "person">
<Xs: complextype>
<Xs: sequence>
<Xs: element name = "name" type = "XS: string" minoccurs = "0"/>
<Xs: element name = "gender" type = "XS: string" minoccurs = "0"/>
</Xs: sequence>
</Xs: complextype>
</Xs: Element>
</Xs: Choice>
</Xs: complextype>
</Xs: Element>
</Xs: schema>
<Diffgr: DiffGram xmlns: msdata = "urn: Schemas-Microsoft-com: XML-msdata"
Xmlns: diffgr = "urn: Schemas-Microsoft-com: xml-diffgram-v1">
<Persontable xmlns = "">
<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>
<Gender> male </gender>
</Person>
<Person diffgr: Id = "person3" msdata: roworder = "2" diffgr: haschanges = "inserted">
<Name> Chris </Name>
<Gender> male </gender>
</Person>
</Persontable>
</Diffgr: DiffGram>
</Dataset>
  
From the above example, we can see that the result of using dataset directly as the return type is quite complex. It not only contains the data in dataset, but also contains the data change information, and dataset schema. Although some tools can generate a client type similar to dataset, it is not clear enough whether to parse complex XML directly or use a class similar to dataset.
  
There are two solutions to this problem:
  
1) construct a custom type with a simple data type, encapsulate a row in the dataset with each custom type object, and return the array of the custom type object to the client; because simple data type definitions are used, the client can completely restore the definition of custom types;
  
2) use the dataset. writexml () method to extract the data in the dataset into XML format and return it to the client as a string. Then, the client parses the XML string to restore the data. Since the redundant information can be filtered out when writexml () is used, the returned content is much simpler than the content in Figure 2.
  
2. Create. NET web services and return the data set
  
With Visual Studio. NET, you can quickly create practical Web services by writing the code of the web method itself:
  
Table 3. XML Web Services implemented with. net
[Webmethod]
Public person [] getpersons ()
{
Person Alice = new person ("Alice", "female ");
Person Bob = new person ("Bob", "male ");
Person Chris = new person ("Chris", "female ");
Person Dennis = new person ("Dennis", "male ");
  
Return new person [] {Alice, Bob, Chris, Dennis };
}
  
[Webmethod]
Public String getpersontable ()
{
Datatable table = new datatable ("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 "});
Table. Rows. Add (New String [2] {"Eric", "male "});
  
Dataset dataset = new dataset ("persontable ");
Dataset. Tables. Add (table );
  
System. Text. stringbuilder strbuilder = new system. Text. stringbuilder ();
Stringwriter writer = new stringwriter (strbuilder );
Dataset. writexml (writer, system. Data. xmlwritemode. ignoreschema );
  
Return strbuilder. tostring ();
}
  
In the above Code, the functions getpersons () and getpersontable () correspond to the two solutions mentioned in "1. Overview" respectively. Specifically, the person type is the custom data type used to encapsulate a row of data in a dataset:

Table 4. Custom type person
[Serializable]
Public class person
{
Public Person ()
{
}
  
Public Person (string name, string gender)
{
This. Name = Name;
This. Gender = gender;
}
  
Public string name = "";
Public String gender = "";
}

The following is the XML format result obtained by directly calling these two web methods in Internet EXPLOERER:
  
Table 5. getpersons () returned results
<? XML version = "1.0" encoding = "UTF-8"?>
<Arrayofperson xmlns: XSD = "http://www.w3.org/2001/XMLSchema"
Xmlns: xsi = "http://www.w3.org/2001/XMLSchema-instance"
Xmlns = "http://tempuri.org/">
<Person>
<Name> Alice </Name>
<Gender> female </gender>
</Person>
<Person>
<Name> Bob </Name>
<Gender> male </gender>
</Person>
<Person>
<Name> Chris </Name>
<Gender> female </gender>
</Person>
<Person>
<Name> Dennis </Name>
<Gender> male </gender>
</Person>
</Arrayofperson>
  
Table 6. string type values returned by getpersontable ()
<Persontable> <person>
<Name> Alice </Name>
<Gender> female </gender>
</Person> <person>
<Name> Bob </Name>
<Gender> male </gender>
</Person> <person>
<Name> Chris </Name>
<Gender> female </gender>
</Person> <person>
<Name> Dennis </Name>
<Gender> male </gender>
</Person> <person>
<Name> Eric </Name>
<Gender> male </gender>
</Person> </persontable>
  
By now, the creation of XML Web Service has been completed .. The XML Web Services created in. Net follow the unified industry standards (such as soap and XML). Therefore, they can be called either in. Net or non-. NET languages. Especially for the results returned by getpersontable (), data can be extracted simply by using XML Parser for processing. The following shows how to complete this process in Java.

This article is reproduced from 1024 K: http://www.1024k.cn/net/2007/200701/15386_2.html
  

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.