To Documents
XML Basics
What is XML?
- XML means Extensible Markup Language
- XML is
a data storage and transmission language
simple, portable, and human readable
hierarchical
self documenting (unlike fixed field or CSV files)
positioned to be the linga franca of data
- .Net uses XML as its preferred internal data representation format.
CSV vs. XML
- Compare these CSV and XML files containing the same data:
CSV Version
Jack,38
Tom,36
XML Version
<NineHoleGolfScores>
<Player ID="348">
<Name>Jack</Name>
<Score>38</Score>
</Player>
<Player ID="541">
<Name>Tom</Name>
<Score>36</Score>
</Player>
</NineHoleGolfScores>
XML is wordy but self documenting.
XML Definitions
An empty element contains only a start tag and an end
tag:
<FirstName></FirstName>
Alternative syntax for an empty element:
<FirstName />
An element with text:
<FirstName>Jerry</FirstName>
/An element with an attribute and text:
<Employee ID="2354">Jerry</Employee>
A nested element:
<Singer ID = 240>
<FirstName>Madonna</FirstName>
<LastName />
</Singer>
Whitespace Elements:
Space
Tab
Line Feed
Carriage Return
Comment Element:
<!-- This is a comment -->
The root element contains all of the data in an XML file.
In the Persons.xml file, Persons is the root element.
An XML file can only contain one root element.
Checking XML Files for Correctness
- Nonvalidating Parsers
Checks if XML file is a well formed document according
to W3C specifications.
- Validating Parsers
Checks the correctness of XML document against
a DTD (Document Type Definition) or XSD
(XML Schema Definition) file.
A Schema can be created in .Net.
- Visual Studio can check an XML file for errors. Open an XML file
using the Visual Studio Editor. If the file is not a legal XML document,
the errors will be indicated. Try it out on this
Persons.xml file.
XML Forward-only Processing
- With forward-only processing, XML elements are processed sequentially.
- No backtracking is allowed.
- Can be done with an XMLTextReader.