|
|
|
|
|
|
Author Rank:
|
|
Total page views :
3044
|
|
Total downloads :
|
|
|
|
|
|
|
Similar ArticlesMost ReadTop RatedLatest
|
|
|
|
|
|
|
|
|
|
This article has been excerpted from book "A Programmer's Guide to ADO.NET in C#".
An XML document is a set of elements in a well-formed and valid standard format. A document is valid if it has DTD associated with it and if it complies with the DTD. As mentioned earlier, a document is well formed if it contains one or more elements and if it follows the exact syntaxes of the language. An XML parser will only parse a document that is a well formed, but the document doesn't necessarily have to be valid. This means that a document must have at least one element (a root element) in it, but it doesn't matter whether it uses DTDs.
An XML document has the following parts, each described in the sections that follow:
- Prolog
- DOCTYPE declaration
- Start and end tags
- Comments
- Character and entity references
- Empty elements
- Processing instructions
- CDATA section
- Attributes
- White spaces
Prolog
The prolog part of a document appears before the root tag. The prolog information applies to the entire document. It can have character encoding, stylesheets, comments, and processing instructions. This is an example of a prolog:
<?xml version ="1.0" ?> <?xml-stylesheet type="text/xsl" href="books.xsl" ?> <!DOCTYPE StudentRecord SYSTEM "mydtd.dtd"> <!=my comments - - - ->
DOCTYPE Declaration
With the help of a DOCTYPE declaration, you can read the structure of your root element and DTD from external files. A DOCTYPE declaration can contain a root element or a DTD (used for document validation). In a validating environment, a DOCTYPE declaration is must. In a DOCTYPE reference, you can even use a URI reference. For example:
<!DOCTYPE rootElement>
or
<!DOCTYPE rootElement SYSTEM "URIreference">
or
<!DOCTYPE StudentRecord SYSTEM "mydtd.dtd">
Start and End tags
Start and end tags are the heart of XML language. As mentioned earlier in the article, XML is nothing but a text file start and end tags. Each tag starts with <TAG> and ends with </TAG>. If you want to add a tag called <book> to your XML file, it must start with <book> and end the </book>, as shown in this example:
<?xml version ="1.0" ?> <book xmlns = "http://www.c-sharpcorner.com/xmlNet"> <title> The Autobiography of Benjamin Franklin</title> <author> <first-name> Benjamin</ First-name> <last-name> Franklin</ last- name> </author> <price> 8.99</ price> </book>
Note: Empty elements don't have to heed this < >...</ > criteria. I'll discuss empty tags later in the "Empty Elements" section.
Note: An element is another name a starting and ending tag pair
Comments
Using comments in your code is good programming practice. They help you understand your code, as well as help others to understand your code, by explaining certain code lines. You use the <! - - and - - > pair to write comments in an XML document:
<!-- My comments here -->
<!-- This is a comment -->
XML parsers ignore comments.
CDATA Sections
What if you want to use < and > characters in your XML file but not as part of a tag? Well, you can't use them because the XML parser will interpret them as start and end tags. CDATE provides the following solution. So you can use XML markup characters in your documents and have the XML parser ignore them. If you use the following line:
<! [CDATA [I want to use < and >, characters]]>
the parser will treat those characters as data.
Another good example of CDATA is the following example:
<! [CDATA [< Title>This is the title of a page</ Title>
In this case, the parser will treat the second title as data as data, not as a mark up tag.
Character and entity reference
In some cases, you can't use a character directly in a document because of some limitations, such as character being treated as markup character or a device or processor limitation.
By using character and entity references, you can include information in a document by reference rather than the character.
A character reference is a hexadecimal code for a character. You use the hash symbol (#) before the hexadecimal value. The XML parser takes care of the rest. For example, the character reference for the Return Key is# x000d.
The reference start with an ampersand (&) and a #, and it ends with a semicolon (;). The syntax for decimal and hexadecimal references is & # value; and &#xvalue; respectively. XML has some built-in entities. Use the It, gt, and amp entities for less than, greater than, and ampersand, respectively. Table 6-2 shows five XML built-in entities and their references. For example, if you want to write a > b or Jack & Jill, you can do that by using these entities:
A>b and Jack& Jill
Table 6-2. XML Build- in Entities
|
ENTITY |
REFERENCE |
DESCRIPTION |
|
Lt |
< |
Less than: < |
|
Gt |
> |
Greater than: > |
|
Amp |
& |
Ampersand: & |
|
Apos |
&apos |
Single quote: ' |
|
Auot |
" |
Double quote: " |
Empty elements
Empty elements start and end with the same tag. They start with < and end with >. The text between these two symbols is the text data. For example:
<Name> </Name> <IMG SRC= "img.jpg" /> <tagname/>
are all empty element example. The <IMG> specifies an inline image, and the SRC attribute specifies the image's location. The image can be any format, though browsers generally support only GIF, JPEG, and PNG images.
Processing Instructions
Processing instructions (PIs) play a vital role in XML parsing. A PI holds the parsing instructions, which are read by the parser and other programs. If you noticed the first line of any of the XML samples discussed earlier, a PI starts like this:
<?xml version ="1.0" ?>
All PIs start with <? And end with ?>. This is another example of PI:
<?xml-stylesheet type ="text/ xsl" href="myxsl.xsl"?>
This PI tells a parser to apply a stylesheet on the document.
Attributes
Attributes let you add extra information to an element without creating another element. An attribute is a name and value pair. Both the name and value must be present in an attribute. The attribute value must be in double quotes; otherwise the parser will give an error. Listing 6-8 is an example of an attribute in a <table> tag. In the example, the <table> tag has border and width attributes, and the <td> tag a width attribute.
Listing 6-8. Attributes in the < table> tag
<table border ="1" width = "43%"> <tr> <td width ="50%">Row1, Column1</td> <td width ="50%">Row1, Column2</td> </tr> <tr> <td width = "50%">Row2, Column1</td> <td width = "50%">Row2, Column2</td> </tr> </table>
White spaces
XML preserves white spaces except in attribute values. That means white space in your document will be displayed in the browser. However, white spaces are not allowed before the XML declaration. The XML parser reports all white spaces available in the document. If white spaces appear before declaration, the parser treats them as PI.
In element, XML 1.0 standard defines the xml: space attribute to insert spaces in a document. The XML:space attribute accepts only two values: default and preserve. The default value is the same as not specifying an xml:space attribute. It allows the parser to treat spaces as in a normal document. The Preserve value tells the parser to preserve space in the document. The parser preserves space in attributes, but it converts line break into single spaces.
Conclusion
Hope this article would have helped you in understanding XML Document and its Items. See other articles on the website also for further reference.
 |
This essential guide to Microsoft's ADO.NET overviews C#, then leads you toward deeper understanding of ADO.NET. |
|
|
|
Login
to add your contents and source code to this article
|
|
|
|
|
|
|
|
|
|
|
|
|
|
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional
consulting company, our consultants are well-known experts in .NET and many of them
are MVPs, authors, and trainers. We specialize in Microsoft .NET development and
utilize Agile Development and Extreme Programming practices to provide fast pace
quick turnaround results. Our software development model is a mix of Agile Development,
traditional SDLC, and Waterfall models.
|
|
Click here to learn more about C# Consulting. |
|
|
|
|
|
|
|
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon.
Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees.
As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
|
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
|
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
|
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today. With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications. Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
|
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or
application via a range of API's. Learn More about our API connections.
|
Microsoft Visual Studio 2010 Professional
Microsoft Visual Studio 2010 Professional will launch on April 12, but you can beat the rush and secure your copy today by pre-ordering at the affordable estimated retail price of $549 (US). Pre-order now.
|
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
|
Developer-Ready ASP.NET 2.0 Web Hosting with 3 MONTHS FREE
Now supporting .NET 3.0 Framework with Windows Workflow Foundation, Windows Communication Foundation (WCF), Windows Presentation Foundation (WPF), windows CardSpace (WCS)! Providing more flexibility for Developers with Web Services Support and a User/Permission Manger. Also supporting MS SQL 2005/2000 with Real-Time Backups, FREE Automated Attach .MDF Tool, FREE SQL Restore and Shrink SQL DB Tools, and SQL
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|