The Extensible Markup Language |
|
Overview of Files |
Consider the following list:
Here is an example of creating and saving such a list: |
using System; using System.IO; using System.Collections; using System.Runtime.Serialization.Formatters.Binary; [Serializable] public sealed class Employee { public string FirstName; public string LastName; public DateTime DateHired; public double HourlySalary; } public static class Program { static int Main(string[] args) { ArrayList lstEmployees = new ArrayList(); Employee contractor = null; contractor = new Employee(); contractor.FirstName = "Johnny"; contractor.LastName = "Watts"; contractor.DateHired = new DateTime(1984, 5, 10); contractor.HourlySalary = 22.85; lstEmployees.Add(contractor); contractor = new Employee(); contractor.FirstName = "Patrick"; contractor.LastName = "Garçon"; contractor.DateHired = new DateTime(2000, 7, 24); contractor.HourlySalary = 14.50; lstEmployees.Add(contractor); contractor = new Employee(); contractor.FirstName = "Ernest"; contractor.LastName = "Simms"; contractor.DateHired = new DateTime(1996, 4, 16); contractor.HourlySalary = 18.05; lstEmployees.Add(contractor); contractor = new Employee(); contractor.FirstName = "Gertrude"; contractor.LastName = "Monay"; contractor.DateHired = new DateTime(2002, 3, 8); contractor.HourlySalary = 26.35; lstEmployees.Add(contractor); FileStream fsEmployees = new FileStream("Employees.mpl", FileMode.Create, FileAccess.Write); BinaryFormatter bfEmployees = new BinaryFormatter(); bfEmployees.Serialize(fsEmployees, lstEmployees); fsEmployees.Close(); return 0; } } When saving this file, we chose an extension (.mpl) at random. After saving the values to a file, at one time, you may want to retrieve the values from that list. Here is an example: using System; using System.IO; using System.Collections; using System.Runtime.Serialization.Formatters.Binary; [Serializable] public sealed class Employee { public string FirstName; public string LastName; public DateTime DateHired; public double HourlySalary; } public static class Program { static int Main(string[] args) { FileStream fsEmployees = new FileStream("Employees.mpl", FileMode.Open, FileAccess.Read); BinaryFormatter bfEmployees = new BinaryFormatter(); ArrayList contractors = (ArrayList)bfEmployees.Deserialize(fsEmployees); fsEmployees.Close(); Console.WriteLine("======================================"); Console.WriteLine("==== Employees Records ==============="); foreach (Employee empl in contractors) { Console.WriteLine("--------------------------------------"); Console.WriteLine("Employee Name: {0}, {1}", empl.LastName, empl.FirstName); Console.WriteLine("Date Hired: {0:D}", empl.DateHired); Console.WriteLine("Hourly Salary: {0:C}", empl.HourlySalary); } Console.WriteLine("======================================\n"); return 0; } } This would produce: ====================================== ==== Employees Records =============== -------------------------------------- Employee Name: Watts, Johnny Date Hired: Thursday, May 10, 1984 Hourly Salary: $22.85 -------------------------------------- Employee Name: Garçon, Patrick Date Hired: Monday, July 24, 2000 Hourly Salary: $14.50 -------------------------------------- Employee Name: Simms, Ernest Date Hired: Tuesday, April 16, 1996 Hourly Salary: $18.05 -------------------------------------- Employee Name: Monay, Gertrude Date Hired: Friday, March 08, 2002 Hourly Salary: $26.35 ====================================== Press any key to continue . . . When you create this type of file, you choose the programming environment of your choice and save the file. You cannot open that file in just any application. You must create and distribute the application to those who want to be able to open a file created by your application. It could be useful to create a type of file using any application of your choice and be able to open that file using another type of application that you may not even know.
We will learn or use XML through the .NET Framework classes. The particularity is that these classes are highly structured to take care of all facets of XML without compromising the standards. In fact, the .NET Framework classes are highly conform to the W3C standards in all areas. To create an XML file, in the document, you type units of code using normal characters of the English language. The XML document is made of units called entities. These entities are spread on various lines of the document as you judge them necessary and as we will learn. XML has strict rules as to how the contents of the document should or must be structured. After an XML document has been created and is available, in order to use it, you need a program that can read, analyze, and interpret it. This program is called a parser. The most popular parser used in Microsoft Windows applications is MSXML, published by Microsoft. A markup is an instruction that defines XML. The fundamental formula of a markup is: <tag> The left angle bracket "<" and the right angle bracket ">" are required. Inside of these symbols, you type a word or a group of words of your choice, using regular characters of the English alphabet and sometimes non-readable characters such as ?, !, or [. The combination of a left angle bracket "<", the right angle bracket ">", and what is inside of these symbols is called a markup. There are various types of markups we will learn. As mentioned above, XML is released as a version. Because there can be various versions, the first line that can be processed in an XML file must specify the version of XML you are using. At the time of this writing, the widely supported version of the .NET Framework is 1.0. When creating an XML file, you should (should in 1.0 but must in 1.1) specify what version your file is conform with, especially if you are using a version higher than 1.0. For this reason, an XML file should start (again, must, in 1.1), in the top section, with a line known as an XML declaration. It starts with <?xml version=, followed by the version you are using, assigned as a string, and followed by ?>. An example of such a line is: <?xml version="1.0"?> By default, an XML file created using Microsoft Visual Studio 2005 specifies the version as 1.0. Under the XML declaration line, you can then create the necessary tags of the XML file. As mentioned already, the tags are created using characters of the alphabet and conform to the ISO standard. This is known as the encoding declaration. For example, most of the characters used in the US English language are known as ASCII. These characters use a combination of 7 bits to create a symbol (because the computer can only recognize 8 bits, the last bit is left for other uses). Such an encoding is specified as UTF-8. There are other standards such as UTF-16 (for wide, 2-Byte, characters). To specify the encoding you are using, type encoding followed by the encoding scheme you are using, which must be assigned as a string. The encoding is specified in the first line. Here is an example: <?xml version="1.0" encoding="utf-8"?>
Due to the high level of support of XML in the .NET Framework, there are various ways you can create an XML file. The most common technique consists of using a simple text editor. In Microsoft Windows, this would be Notepad. An XML file is first of all a normal text-based document that has a .xml extension. Therefore, however you create it, it must specify that extension. Many other applications allow creating an XML file or generating one from an existing file. There are also commercial editors you can get or purchase to create an XML file. |
|
||
Home | Copyright © 2006-2016, FunctionX, Inc. | Next |
|