. NET Learning Notes---XML basics

Source: Internet
Author: User
Tags cdata closing tag opening and closing tags xml attribute xml cdata xml example xml parser

I. Introduction to XML

XML is a markup language that describes data and provides a standardized way to represent text data. The XML document is suffixed with. Xml. It is important to note that XML is case-sensitive.

Let's start with a simple XML example to understand the underlying XML:

<?xml version= "1.0" encoding= "Utf-8"? ><books isbn= "9787544238212" >  <title>xml Learning Notes </title >  <price>30</price>  <pagecount>300</pagecount></books>

The first line shows that the version that this XML adheres to is 1.0, and the character encoding used is the UTF-8 character set.

Each XML element has a start tag and ends with a closing tag. For example start: <title>, end:</title>. XML elements can have attributes, and attribute values are quoted. For example, the ISBN in the above example is a property, and the attribute value is enclosed in double quotation marks. Connect with an equal sign at the same time.

An XML document can have only one root node, as in the example above books is the root node.

The following are some of the key points in XML:

    1. XML is an Extensible Markup language;
    2. XML tags are not predefined, and users can define their own tags to describe the data;
    3. XML is mainly used to describe and store data;
    4. XML has a self-descriptive nature;
    5. XML is a tree-like document and a structured document;
    6. An XML document is a text file;
Second, XML syntax
    1. Each start tag must have an end tag;
    2. An XML document can only have a single root element;
    3. All XML elements must be nested correctly;
    4. Attribute values must be quoted;
    5. An element cannot have the same attribute;

The following example explains the common terminology of XML.

  1. Marking

Tags are divided into opening and closing tags, such as <title> is a start tag. </title> is an end tag. The content between the opening tag and the closing tag becomes the content of the XML element. If an XML element has no content, we call it an empty element. such as <title></title>.

  2. Elements

An XML element is a piece of content from a start tag to its end tag. For example, <title>xml study notes </title> is an element.

  3. Properties

An element can have attributes that are written in the start tag and written behind the element name. For example <books isbn= "9787544238212", where isbn= "9787544238212" is a property of the books element. ISBN is the name of the property, and 9787544238212 is the property value. An XML element cannot have the same XML attribute name.

Third, the relationship between XML elements
    1. Child nodes;
    2. parent node;
    3. Parallel node (sibling relationship);

Here's an example to explain the three relationships.

<?xml version= "1.0" encoding= "Utf-8"? ><father>tom Smith  <son>    John Smith    < Grandson>hans smith</grandson>  </son>  <daughter>jane smith</daughter></ Father>

In the example above, the son element is the child node of the Father element. The Father element is the parent node of the son element and the daughter element. The son element and the daughter are also called parallel nodes.

XML elements can have attributes as additional information for XML elements.

If the attribute value contains double quotation marks, enclose the attribute value in single quotation marks. Such as:

<site info = ' wo ' ai ' ni ' >

If the attribute value contains single quotation marks, enclose the property value in double quotation marks. Such as:

<site info = "Wo ' ai ' ni" >

An XML element can have one or more attributes, with each attribute separated by a space. Such as:

<person name= "Zhangsan" age= ">"
Iv. naming rules for XML names

The name of the XML can contain English letters and numbers, or other characters such as underscores. XML names cannot begin with numbers or punctuation, and XML cannot begin with XML (or the case of XML, because this is a reserved term for XML-related standards). XML names cannot contain spaces, although XML names support underscores (_), hyphens (-), and periods (.). and colons (:), but cannot use hyphens (-), periods (.) At the beginning of the XML. and a colon (:).

1. XML Entity Application

  (1), in the XML tag in addition to the beginning of a token, is not allowed to have less than <, because the less than sign is always interpreted by the XML parser as the beginning of a tag.

Such as:

< </person>

This line of code is wrong, if you must write in the contents of the XML element less than, then you can use less than the entity reference, that is:&lt; to replace the less than sign.

Such as:

&lt; 10</person>

is the correct wording.

  (2) The & character cannot be used in XML, because & is interpreted as the beginning of an entity reference. So you must use & 's entity references &amp; To replace &.

Such as:

    • <person>a & B</person> is wrong
    • <person>a &amp; B</person> is right.

XML5 a predetermined entity reference is given below

Entity reference Character Description
&lt; < Less than sign
&gt; > Greater than sign
&amp; & And
&apos; Single quotation marks
&quot; " Double quotes

When the XML parser parses an XML document that contains the above entity references, the entity references are converted to the corresponding characters.

Only < and & are illegal in XML. The other several are legal and can be written directly, only with the entity reference clearer.

  2. XML CDATA Zone

When you need to add a piece of content to an XML document, and there's a lot of < or & in the content, it's a hassle to convert all < or & to entity references, so you can use CDATA zones, and you don't have to use entity references in CDATA areas. Because the XML parser does not calculate the content in the CDATA area.

CDATA area with <! [cdata[begins, ends with]]>

Examples are as follows:

<mycode> <! [cdata[

Note: Within the CDATA area, cannot appear]] >

  3. XML annotations

The XML comment starts with <!--and ends.

Example:

<!--This is a comment--

In the XML comment, except for the Terminator-->, two hyphens cannot appear--。 XML annotations can also be placed in element content.

  4. XML declaration

XML documents should start with an XML declaration, but are not required.

Example:

<?xml version= "1.0" encoding= "Utf-8" standalone= "yes"?><person>    Zhang San </person>

If an XML document contains a declaration, it must be placed in the first line of the XML document.

The XML document declaration has three main parameters.

    1. Version: Version indicates that the XML1.0 standard is followed.
    2. Character encoding: The character encoding represents the character encoding used by the XML document. For Chinese documents, you should use UTF-8 or Unicode. If it is in plain English, you can make iso-8859-1 code.
    3. Standalone: If the value of XML standalone is no, it means that it requires a DTD. XML documents that do not require a DTD, the value of standalone should be written as yes.

  5. A properly formatted XML document.

    • Each start tag must have an end tag.
    • The XML document has only one root element.
    • XML elements must be nested correctly.
    • XML elements cannot have properties of the same name.
    • XML attribute values must be quoted.
    • The XML comment cannot be unloaded in the tag.
    • < and & tags cannot appear in the XML document. The entity reference must be used instead.

  6. XML namespaces

XML namespaces are introduced to avoid conflicts when there are multiple elements of the same name in an XML document.

Syntax for namespaces:

    • xmlns:prefix= "URI"
    • xmlns: Represents a required property.
    • Prefix: Represents the alias of the namespace, and its value cannot be XML.
    • URI: A compact string used to identify an abstract or physical resource. To ensure identity uniqueness, XML uses URLs on the internet as the identifier, because URLs are unique.

Example of the default namespace:

<?xml version= "1.0" encoding= "Utf-8"? ><article xmlns= "http://www.xxx.com/" > <!--default namespace  - <author age= "> Zhang San </author>  <length>12000</length>  <price>42</price ></Article>

Namespace with Name |:

<?xml version= "1.0" encoding= "Utf-8"? >

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.