XML syntax
Here we will introduce XML schemas:
With the increasing popularity of XML Schema specifications, it is not long before W3C standards are recommended. Let's take a look at this new method for defining document types.
I. Definition Elements
Most of the work of document type definition is now done by dtds. Currently, DTD has some functional limitations. As XML applications become more and more extensive, these restrictions gradually hinder the development of XML.
The DTD syntax is different from the XML syntax. Therefore, document writers must learn another set of symbolic languages. The software also needs another parser to parse the DTD file.
In DTD, there is no way to define the data types and formats that can be directly mapped from program language variables to XML data. There is no basic set of elements that are well-known for document writers to choose from. Dtds is inherited from its predecessor SGML (in fact, XML itself is a simplified version of SGML). This relatively mature technology can quickly run XML, in addition, they can make people familiar with SGML feel more about XML. However, people soon discovered that XML requires a more expressive solution than a DTD.
To define an element, you must specify the element name, content mode of the element, attributes of the element, and embedded sub-elements. In XML schemas, the content mode of an element is defined by type. An XML document that is subject to a specific schema can only be written in accordance with the element schema defined in the schema, which is the same as the DTD rules. Elements can be simple or complex. Many simple types are defined in the schemas specification, such as string, integer, and decimal. Simple elements cannot contain child elements and attributes, while complex elements can nest child elements and contain attributes.
Let's take a look at an example of a simple definition element:
<Element name = "quantity" type = "positive-integer"/>
<Element name = "amount" type = "decimal"/>
We know that there is a concept of aggregation and inheritance in the object-oriented perspective, and new classes can be derived from existing classes. Here, the schema uses these ideas to define new elements based on the old elements by clustering and inheritance. Clustering can combine a group of existing elements into a new element. Inheritance defines a new element by extending an existing element, and the new element can represent the inherited element.
For example, if we want to derive a new value element from the decimal type and make it have this content mode: <value unit = "Celsius"> 42 </value>, we can define this as follows:
<Element name = "value">
<Complextype base = & single; decimal & single; derivedby = & single; Extension & single;>
<Attribute name = & single; Unit & single; type = & single; string & single;/>
</Complextype>
</Element>
Next we will aggregate the time and value elements into a measurement element to form this content mode:
<Measurement>
<Time> 2000-10-08 12:00:00 GMT <time/>
<Value unit = "Celsius"> 42 </value>
The final definition result of our schema element should be as follows:
<Element name = & single; Measurement & single; type = & single; Measurement & single;/>
<Complextype name = & single; Measurement & single;>
<Element name = & single; Time & single; type = & single; Time & single;/>
<Element name = & single; Value & single; type = & single; Value & single;/>
</Complextype>
Equivalent to the schema above, the DTD is:
<! Element measurement (time, value)>
<! Element time (# pcdata)>
<! Element value (# pcdata)>
<! ATTLIST value (unit)>
Obviously, it means much less.
The inheritance feature introduced from object-oriented languages such as Java also includes the ability to define abstract elements to force the implementation of subclass elements, or to define an ending element to prohibit elements from being inherited by other elements. This makes it more intuitive and feasible to map elements to classes in Java or C ++.
Ii. Base expression
XML schema is more convenient to express the concept of element base than DTD. The base number refers to the number of times an element appears in the document. In DTD, a regular expression is used to represent the base number, while the expression ability of regular expressions is the lowest in all formal grammar, this makes it difficult to define documents. You can specify only one (1), zero or more (*), one or more (+) three bases, and use these base sequences to form a DTD. In XML schema, The minoccurs and maxoccurs attributes are used to define the element base, respectively, to specify the minimum number and maximum number of occurrences of the element:
<Element ref = "optionalelement" minoccurs = "0"/>
<Element ref = "twoormoreelements" minoccurs = "2" maxoccurs = "unbounded"/>
<Element ref = "exactlyoneelement"/>
The default values of minoccurs and maxoccurs are both 1. That is to say, when these two attributes are not specified, the element can only appear once in the document. In addition to these two attributes, you can also use the choice and all elements. The element choice only allows one of all its child elements to appear in the document. The element all is the loose, allowing all its child elements to appear in any order of any number of times in the document. These concepts are hard to express in DTD.
<XSD: Choice>
<Element ref = "eitherthis"/>
<Element ref = "orthat"/>
</XSD: Choice>
<XSD: All>
<Element ref = "ying"/>
<Element ref = "yang"/>
</XSD: All/>
Iii. Domain Name
Domain names are also supported in XML schema. In addition to defining the XML document vocabulary, a schema can also define the target domain name and other vocabulary domain names that may be used. For example:
<XSD: schema targetnamespace = & single; http://www.physics.com/measurements&single; xmlns: XSD = & single; using xmlns: Units = & single; http://www.physics.com/units&single;>
<XSD: element name = & single; units & single; type = & single; units: Units & single;/>
<XSD: element name = & single; Measurement & single; type = & single; Measurement & single;/>
<Complextype name = & single; Measurement & single;>
<Element name = & single; Time & single; type = & single; Time & single;/>
<Element name = & single; Value & single; type = & single; Value & single;/>
</Complextype>
This mechanism provides a modular and easy-to-expand method for building a complex domain name system. So that the role of the domain name can be truly reflected.
XML Schema provides a rich and more flexible mechanism to define the XML document vocabulary. It uses the XML language itself to define meta-information about an XML document, which greatly enhances the XML collaboration capability. Many improvements and enhancements to the DTD function make it end with a standard of XML. There are already many schema tools on the Apache project and the home page of IBM alphaWorks. If you are interested, go and have a look.
Now, let's take a look at how to read data from an XML file. The dataset control provides the readxml method. At the same time, the XML file must contain the schema and the data we need ).
Now, let's take a look at the data1.aspx file:
Source File: advanceapp/data1.aspx
<% @ Import namespace = "system. Io" %>
<% @ Import namespace = "system. Data" %>
<HTML>
<Head>
<Title>
XML demonstration
</Title>
</Head>
<Script language = "VB" runat = "server">
Sub page_load (SRC as object, e as eventargs)
Dim ds as new dataset
Dim FS as filestream
Dim reader as streamreader
FS = new filestream (server. mappath ("data1.xml"), filemode. Open, fileaccess. Read)
Reader = new streamreader (FS)
DS. readxml (Reader)
FS. Close ()
Dim source as dataview
Source = new dataview (Ds. Tables (0 ))
Myspan. innerhtml = source. Table. tablename
Mydatagrid. datasource = Source
Mydatagrid. databind ()
End sub
</SCRIPT>
<Body>
<Center>
<H3> <font face = "verdana"> XML demonstration <span runat = "server" id = "myspan"/> </font> <Asp: DataGrid id = "mydatagrid" runat = "server"/>
</Center>
</Body>
</Html>
Let's take a look at the content of the XML file (advanceapp/data1.aspx:
The data1.xml file is as follows:
<Center>
<Root>
<Schema id = "documentelement" targetnamespace = "" xmlns = "http://www.w3.org/1999/XMLSchema" xmlns: xdo = "urn: Schemas-Microsoft-com: XML-xdo" xdo: datasetname = "documentelement">
<Element name = "student">
<Complextype content = "elementonly">
<All>
<Element name = "name" type = "string"> </element>
<Element name = "Age" type = "int"> </element>
<Element name = "sex" type = "string"> </element>
<Element name = "Grade" type = "string"> </element>
</All>
</Complextype>
</Element>
</Schema>
<Documentelement>
<Student>
<Name> Jimmy </Name>
<Age> 20 </age>
<Sex> boy </sex>
<Grade> freshman </grade>
</Student>
<Student>
<Name> Mary </Name>
<Age> 20 </age>
<Sex> girl </sex>
<Grade> sophomore </grade>
</Student>
<Student>
<Name> Tom </Name>
<Age> 19 </age>
<Sex> boy </sex>
<Grade> freshman </grade>
</Student>
<Student>
<Name> Susan </Name>
<Age> 20 </age>
<Sex> girl </sex>
<Grade> freshman </grade>
</Student>
</Documentelement>
</Root>
</Center>
The demo is as follows:
Of course, we can split the schema and data into opposite files. In the main file, we need to use the readxmldata and readxmlschema methods respectively.
For example:
Read the contents of Schema. xml:
FS = new filestream (server. mappath ("schema. xml"), filemode. Open, fileaccess. Read) Schema = new streamreader (FS)
DS. readxmlschema (schema)
FS. Close ()
Read data. xml:
FS = new filestream (server. mappath ("data. xml"), filemode. Open, fileaccess. Read)
Reader = new streamreader (FS)
DS. readxmldata (Reader)
FS. Close ()
The data1.xml mentioned above is divided into two parts:
Part 1: generate the schema. xml file:
<Schema id = "documentelement" targetnamespace = "" xmlns = "http://www.w3.org/1999/XMLSchema" xmlns: xdo = "urn: Schemas-Microsoft-com: XML-xdo" xdo: datasetname = "documentelement">
<Element name = "student">
<Complextype content = "elementonly">
<All>
<Element name = "name" type = "string"> </element>
<Element name = "Age" type = "int"> </element>
<Element name = "sex" type = "string"> </element>
<Element name = "Grade" type = "string"> </element>
</All>
</Complextype>
</Element>
</Schema>
The second part generates data. xml:
<Documentelement>
<Student>
<Name> Jimmy </Name>
<Age> 20 </age>
<Sex> boy </sex>
<Grade> freshman </grade>
</Student>
<Student>
<Name> Mary </Name>
<Age> 20 </age>
<Sex> girl </sex>
<Grade> sophomore </grade>
</Student>
<Student>
<Name> Tom </Name>
<Age> 19 </age>
<Sex> boy </sex>
<Grade> freshman </grade>
</Student>
<Student>
<Name> Susan </Name>
<Age> 20 </age>
<Sex> girl </sex>
<Grade> freshman </grade>
</Student>
</Documentelement>
7.1.4 Summary
Through the above introduction, we have a certain understanding of XML in ASP. NET. In the following sections, we will give a more in-depth explanation of XML.
Advertisement bar Creation
In this program, different advertisement bars are displayed each time we access a webpage through the XML language. In this example, we only call two advertisements.
Source File: advanceapp/intro. aspx
The intro. aspx code is as follows:
<HTML>
<Center> <title> ad bar demonstration </title> </center>
<Head>
<LINK rel = "stylesheet" href = "intro.css">
</Head>
<Body>
<Center>
<Form action = "intro. aspx" method = "Post" runat = "server">
<H2> ad bar demonstration </H2>
<Asp: adrotator advertisementfile = "intro. xml" bordercolor = "black" borderwidth = 1 runat = "server"/>
</Form>
</Center>
</Body>
</Html>
The intro. XML Code is as follows:
<Advertisements>
<Ad>
<Imageurl>./hp1.gif </imageurl>
Http://www.pidsoft.com.cn <navigateurl> </navigateurl>
<Alternatetext> welcome! </Alternatetext>
<Keyword> computers </keyword>
<Impressions> 80 </impressions>
</Ad>
<Ad>
<Imageurl>./hp2.gif </imageurl>
Http://www.pidsoft.com.cn <navigateurl> </navigateurl>
<Alternatetext> welcome to </alternatetext>
<Keyword> computers </keyword>
<Impressions> 80 </impressions>
</Ad>
</Advertisements>
In intro. in aspx, we use <asp: adrotator advertisementfile = "intro. XML "bordercolor =" black "borderwidth = 1 runat =" server "/> this statement is embedded in intro. XML file. When we click Refresh and press the dig or F5 key, we will see another advertisement bar.
In this example, we use the adrotator server control. In the XML file, we can define its attributes, as shown in the table:
Attribute description
Absolute or relative address of the imageurl Image File
Navigateurl: When an image is clicked, you can access the corresponding webpage.
Alternatetext a prompt is displayed when you move the cursor over the image.
Keyword specifies the classification of advertising bars. We can use this attribute to classify advertising bars.
Impressions specifies the size of the image in the table.
XML and dataset controls
Data access is the core of an application system. The common language runtime environment provides methods for managing APIs for data access. These APIs can extract the required data regardless of the data source, such as SQL Server, oledb, and XML. During specific programming, three objects are often used: connections, commands, and datasets.
Object Description
Connect to the data source. For example, SQL Server or an XML file.
Command to manipulate the data. For example, delete, select, and update)
Dataset displays the required data
To enable SQL statements, we must first import the system. Data and system. Data. SQL namespaces.
The statement is as follows:
<% @ Import namespace = "system. Data" %>
<% @ Import namespace = "system. Data. SQL" %>
The following table describes several typical SQL statements:
SQL statement example
Select (operations on a single table) Select * from student where stuname = 'lily ';
Select (operations on multiple tables) Select * from student s, DEPT d Where S. Dept = D. Dept;
Insert into student values ('John ', 21, 'male ');
Delete Delete from student where name = 'John ';
Update student set age = 21 where name = 'lily ';
Before executing a query, we need to construct a sqldatasetcommand object. After executing the query, We need to transfer the data to dataset. We can use the following code:
Suppose we have a test database, which has a student table.
Dim myconnection as new sqlconnection ("Server = localhost; uid = sa; Pwd =; database = test ")
Dim mycommand as new sqldatasetcommand ("select * from student", myconnection)
Dim ds as new dataset ()
Mycommand. filldataset (DS, "student ")
Readers may wonder: why should I use XML files to store data? Why not use the database?
This is because it is too wasteful to use databases for many purposes .. To use a database, you must install and support a separate server processing process (a separate server process), which usually requires an administrator to install and support it ). You must learn SQL statements, use SQL statements to write queries, convert data, and return data. If you use an XML file to store data, you can reduce the load on the server. Also, you have found a simple way to edit data. You only need to use a text editor instead of complex database tools. XML files can be easily backed up, shared with friends, or downloaded to your client. Similarly, you can easily upload new data to your site through FTP.
XML also has a more abstract advantage, that is, as a hierarchical format is better than relational. It can be used directly to design the data structure to meet your needs. You do not need to use an entity-link editor or standardize your chart schema. If one element contains another element, you can directly represent it in the format without using Table Association.
Note: In many applications, relying on file systems is insufficient. If there are many updates, the file system will be damaged due to simultaneous writing. Databases usually support transaction processing and can cope with the requests that occur without being damaged. Complex query statistics must be updated repeatedly and in a timely manner, and the database performance is excellent at this time. Of course, relational databases also have many advantages, including rich query languages, graphic tabulation tools, scalability, and access control.
In the following example, as for most small and medium-sized sites based on published information, most of the data you may involve is read rather than write. Although the data may be large, but it is not updated frequently, and you do not need to perform complicated queries. Even if you need to do so, you will also use an independent query tool, the advantages of mature RDBMS disappear, while those of Object-oriented Data models can be reflected.
Finally, it is entirely possible to provide a queryer shell for your database to perform SQL queries and convert them into an XML Stream.
So you can choose either of the two methods. XML is becoming a very robust and easy-to-program tool. It is used as a front-end tool for storage and query of a mature database. (Oracle XSQL servlet is a good example of this technology .)