This is normal text.
My link Value 123 Andrew The one on the left is a simple web page written in HTML, including the title, a heading, a paragraph, and a hyperlink The file on the right is an XML file containing a number of properties for a log file Both contain a series of values enclosed in matching tags to identify the type of data that is represented The big difference between the two is that HTML is a highly specialized markup language aimed at a particular purpose — to describe the format of a web page XML, on the other hand, is a generic language designed to describe any kind of data XML has no predefined tags like those in HTML Instead, most XML files are defined by a definition document of some kind There are two kinds of definition files: Document Type Definition (DTD) and XML Schema Documents (XSDs) To keep this discussion brief, the focus here will be on XSD definitions because they are usually used when using XML in NET Your XML file does not require an XSD to accompany it, but if it contains more than a couple of values and you’re depending on the XML contents following a set structure so that you can read it in your application, you are better off using an XSD to keep the data in order This is because most XML processing systems, including the one that comes with Visual Basic Express, can validate the XML data against the XSD and produce errors that you can check if the data is not valid The makeup of an XML file is straightforward Each matching pair of tags is called an element or a node Therefore, in the earlier sample XML file, you have a config node, which contains a Values node and a State node The Values node in turn contains two Setting nodes The information between the opening and closing tag is known as the value The two Setting nodes have the values Value and 123, respectively Finally, within the opening tag of an element can be a number of properties that belong to the node; these are known as attributes The User node has an attribute of Login with a value of True The whole thing is called an XML document Besides this simple structure, you have to follow some basic rules when creating an XML file When you are using the classes and methods in Visual Basic Express, it does most of the work for you, but you can still produce invalid XML data if you don’t follow these guidelines First, you must have only one root element that contains the rest of the XML document within its opening and closing tags While you could define multiple nodes at the top level and read them using custombuilt programming logic, the XML standard specifies that there be only one 242 Using XML You must include the closing tag in the pair HTML and other markup languages sometimes allow you to omit the closing tag, and implicitly assume them, but XML is stricter than that If the XML node doesn’t contain any data, you can shortcut the opening and closing tag by closing the node off in the first tag with a single slash (/) For example, the following two lines are considered identical by an XML processor: Unlike Visual Basic Express variables and class names, XML tags are case sensitive Therefore, if your opening tag is called , you must close the pair with exactly the same case — won’t cut it When attributes are defined within a XML tag, the values must be enclosed in quotation marks, even when the values are numeric or single words HTML allows you to omit the quotation marks in these simple-value cases Extensible Means Just That One great advantage of XML is that you can extend the data definition without breaking your application This is because the application can still find the nodes it used prior to the data change For example, the original XML definition used by your application is as follows: Value 123 Andrew Your program uses the Setting nodes and the User node to display some information on a form Now, the other program that created the XML file is extended and includes additional information: Value 123 Andrew C:\Temp\MyLog.txt Your code would still be able to access the Setting nodes and the User node without needing to know anything about the new data being stored in the file The same thing applies to additional attributes in a node 243 Chapter 12 When referring to nodes, XML uses a family-oriented nomenclature This enables you to easily determine how nodes relate to each other The node that owns another is known as the parent element of the other node, while the one that is owned is the child element of the first Nodes that are on the same level within a single parent element are called siblings, or sometimes sister elements To illustrate this, in the case of the sample XML that you’ve been looking at, the Setting nodes are child elements of the Values node, and the State node is the parent element of the User and File nodes User and File are siblings of each other but are not siblings of the Setting nodes XML Attributes Each XML element can have its own attributes Again, these attributes can be controlled by a definition file so that only allowed property names and values can be included, but because you usually own the definition file as well, you can dictate which attributes you want to have defined The first line of the sample XML file defines the root element It has a name of config and two attributes — version and time As mentioned earlier, every attribute value must be enclosed in quotation marks XML allows you to use either single or double quotes, so both version=”1.0” and version=’1.0’ are deemed acceptable Usually attributes are used by the program to determine what to with the data stored within the XML element The User node has a value of Andrew and an attribute of Login with a value of true The application could use this information to determine that the user involved in the process was named Andrew, and that he was logged into the system at the time The Login attribute wasn’t necessary to identify the User, but provided additional information that the program could use There is no hard-and-fast rule about when to store information in an attribute, when to use a child element, or when to include the data in the value component of the element The User node could be rewritten as follows: true Andrew It could even have been defined with what is known as mixed content (whereby the element has a value and child elements) like so: Andrew true Validating Data An XML Schema Document, known as a XSD, is a definition file used to determine whether the XML data is valid You can have an XML file that is well formed, a term used to indicate that all nodes have their opening and closing tags, attributes are properly defined, and so on, that is still not valid A valid XML document is one that conforms to a data definition — either a DTD or an XSD 244 Using XML Each element within the XML must be defined in the schema; otherwise, the XML document is considered invalid The XML file that’s been used as an example could be defined with the following XSD: You might have noticed that the XSD itself looks like XML, and that’s because it is XSD files must conform to their own data definition layout specified in the standard for XML schema In fact, this sample XSD file contains the location of the namespace that defines its own structure — http://www.w3.org/ 2001/XMLSchema When you look through this schema, each element can be seen as an xs:element node that has attributes describing its use and type For example, the config node has a type of configType, which is then defined in the following lines in the file While this book isn’t aimed at teaching you XML, the previous discussion should serve to help you get a basic understanding of how it works so you can look at the way Visual Basic Express uses XML and takes advantage of it If you need to know more, you can find plenty of resources for writing XML and XSDs, including Beginning XML, 3rd Edition, by David Hunter et al (Wiley, 2004) Databases and XML One feature of Visual Basic Express is its capability to export information stored in a SQL Server database to an XML file This can then be accessed by other applications that not have access to your database You can also populate a database table from XML files, too 245 Chapter 12 Before you look at the main XML objects found in Visual Basic Express, these capabilities to convert SQL Server data to and from XML should help you understand how an XML file might be used in your own applications The DataTable class has two methods — ReadXml and WriteXml — both with multiple definitions: ❑ ReadXml is the simplest because it just needs to know where to get the data from and then works out the rest The different definitions of ReadXml simply take different parameters to indicate the data source The syntax of ReadXml is MyTable.ReadXml(DataSource), where DataSource can be a filename, an IOStream, an XMLReader or a TextReader The filename is the most basic and easiest to use If you try to read XML data that does not meet the DataTable’s own definition, an exception is raised Otherwise, the DataTable contents are replaced with the information stored in the XML file ❑ The WriteXml method of the DataTable object has an overwhelming number of overloaded definitions Overwhelming, that is, until you realize they are just variations on a theme In fact, you have only a few options, but each can be used in conjunction with a different set of other parameters The first parameter defines the type of output object that will be written to This is similar to the parameter of ReadXml — IOStream, TextWriter, XMLWriter, or filename In addition to this are two optional parameters, a Hierarchy Boolean value and a WriteMode value The Hierarchy flag dictates whether the WriteXml command includes all child tables or just the main table that the DataTable object contains This could be useful if you have a single DataTable object with a collection of tables stored within it The WriteMode tells the WriteXml what information to include with the actual data of the table When Visual Basic Express creates the XML file, it can include the data as is — this is, the default behavior However, you can also specify that it should include an XSD along with the data so that any application reading the XML knows how to validate it and what each element is supposed to contain Finally, you can specify a WriteMode of DiffGram This tells WriteXml to write only the parts of each row in the table that have changed This can be useful for logging database changes because it excludes any records of information that have not changed since the last database update To confirm the information just discussed, the following Try It Out adds export and import functionality to the Personal Organizer application using XML data files Try It Out Exporting and Importing XML Start Visual Basic Express and open the Personal Organizer application project you’ve been working on If you don’t have an up-to-date version of the project, you can find one in the Code\Chapter 12\Personal Organizer Start folder of the code you downloaded from www.wrox.com You have two functions to implement: exporting the data from the database into an XML file and importing an XML file back into the database The first feature is quite straightforward to implement, with only one gotcha to be aware of, but importing has a number of other issues that you’ll see in a moment 246 ... most basic applications Items such as the Windows system message queues, performance counters, and Active Directory entries can all be accessed via components available to you in Visual Basic Express. .. this simple structure, you have to follow some basic rules when creating an XML file When you are using the classes and methods in Visual Basic Express, it does most of the work for you, but... XML, the previous discussion should serve to help you get a basic understanding of how it works so you can look at the way Visual Basic Express uses XML and takes advantage of it If you need to