Apply XML Schema file

Source: Internet
Author: User
Tags format comparison sort
xml| architecture work, sometimes the problem of data interaction between systems is encountered. The method I have done before is that the source system exports the data as a text file (txt suffix), and then uploads it to the target system, analyzing and reading it. Now a bit better, the application of Web service, write a data interface, in the program directly call, and no need to manually upload a process. And the data that the Web service transmits, all is the XML format, can be very convenient read, do not have to write the analytic algorithm by oneself again. Of course, because it is in XML format, each field value before and after each mark, and is clear, if a lot of records, you may need to transfer the amount of data is relatively large, this may be not good enough place.

Because the XML file stores only data and does not have a data type description, reading data from an XML file may result in data types that do not match the original. For example, there is a table of EMPLOYEE, which is structured as follows: ID (INT), NAME (varchar (50)), expressed in XML as <EMPLOYEE><ID>1</ID><NAME> John < /name></employee>. In this way, the type of ID is originally an integral type, and at this time the system thought it was character type!

Because of this situation, we have a bit of trouble.

When data is imported, it is often analyzed to compare the imported data with the existing data to determine whether to insert, update, or delete. Comparison of the algorithm, the simplest of course is to two datasets (dataset) to a nested loop, one record comparison. However, this method is not efficient, can be improved, the two data sets are sorted, and then compared.

How to sort it? The data is read from the XML file, stored in the dataset Dsxml, and the existing data is stored in the dataset Dscur with only one DataTable in each dataset. You can sort by using the Select method of the DataTable, as follows:

Reading data from an XML file

DataSet Dsxml = new DataSet ("Dsxml");

Dsxml.readxml ("Employee.xml");

Reads data from an existing database. GetData is a custom function that extracts data from an existing database

DataSet dscur = GetData ();

Sorted in order of ID.

The first parameter of the Select is the filter condition, where null indicates that all records fit; The second parameter represents the Sort field

datarow[] Drsxml = dsxml.tables[0]. Select ("", "id");

datarow[] drscur = dscur.tables[0]. Select ("", "id");

Then compare the two recordsets: Drsxml and Drscur.

But the results of the operation were quite different from the original idea.

What's the reason?

That's because in Dsxml, the field ID is a character type. Character sorting is different from numeric ordering, and if you let the system compare, "9" is definitely greater than "10". Therefore, the order of Dsxml does not get the effect we expected.

It seems that the system has to know that the ID is integral type. How can it be done? You can combine XML Schema files.

Dsxml.readxmlschema ("employee.xsd");

Dsxml.readxml ("Employee.xml", Xmlreadmode.inferschema);

After reading this, the data structure is exactly the same as that described in the architecture file employee.xsd.

To add, I am unfamiliar with the syntax of the schema file, the first is to use the data set from the database Dscur output, and then slightly modified:

Dscur. WriteXmlSchema ("employee.xsd");



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.