Tuesday, February 15, 2011

XML in Visual Studio


XML stands for EXtensible Markup Language
XML is a markup language much like HTML
XML was designed to carry data, not to display data
XML tags are not predefined. You must define your own tags
XML is designed to be self-descriptive
XML is a W3C Recommendation

    Extensible Markup Language (XML) is an open-standards cross-platform way of specifying documents. At its origins, XML was used to represent data, but it has grown in use to include user interface technologies and even executable logic. While there are many practical uses of XML, this book is mostly concerned with explaining how XML is used for ASP.NET, Silverlight, and Windows Presentation Foundation (WPF).

    In each of these scenarios, some specialization of XML is being used to construct user interfaces. In ASP.NET, you use XML for HTML (XHTML). Both Silverlight and WPF use XML Application Markup Language (XAML), pronounced “Zamel.” Before learning about XHTML or XAML, you might want an introduction or refresher on XML

    VS 2010 XML Editor
    You can create your own XML documents in VS 2010 with the XML editor. There are a couple of ways to open a new XML document, within or without a project. Without a project, select File | New | File and select XML File, and click OK. You can rename the file (for instance, Customer.xml) when saving. Within a project, right-click the project, select Add | New Item, select the Data list, select XML File, give the document a name (for instance, Customer.xml), and click OK. What this gives you is an editor with Intellisense support that is better than Notepad. Listing A-1 shows an XML document that holds customer data.

    Listing 1: An XML document example
    <?xml version="1.0" encoding="utf-8" ?>
    <customer id="7">
    <address>123 4th St</address>
    As you can see in Listing 1, an XML document is readable text. It contains data, and the meaning of that data is specific to the applications that need to use it. The following sections will decipher Listing 1 and explain what each part of the document means.

    XML Prefixes
    The top of the document in Listing 1 contains an XML prefix, repeated here for convenience:
    <?xml version="1.0" encoding="utf-8" ?>

    The prefix is common for letting applications reading the document know that it is indeed an XML document. The version is self-describing. Encoding is important because it specifies the binary format of the text. If you have one application passing data to another application, it’s important that both applications can read the document and are using the same encoding. The utf-8 encoding is the default and for the purpose of this article is the only encoding you will care about.
    The angle brackets, < and >, define the markup in XML. For the file prefix, content is placed between <? and ?> character sequences, but as the following sections show, most other markup is different.

    XML Elements
    The XML elements in Listing 1 are customer, name, and address. Each element is defined by matching pairs of markup, following this pattern:


    In the previous example, elementName is the name of the element and value is the data associated with that element. Elements always have a begin tag and an end tag. You can identify the end tag because it always follows the begin tag eventually (there may be other element tags nested in between the pair) and contains a forward slash character before the element name. The value in the previous example can sometimes be blank, meaning there is no value for that element. A value can also be one or more elements, such as customer, in Listing A-1, which contains name and address elements. In Listing 1, the value of name is Joe and the value of address is 123 4th St. In addition to elements, you can have attributes, discussed next.

    An attribute decorates an element with a single value, such as in the following example:

    <elementName attributeName="attributeValue">
    Notice that the attribute, attributeName, is inside of the start tag of the element. It contains an equal sign and a quoted value. You can have multiple attributes on a single element and they’ll be separated by spaces. Remember that attributes can have only one value, but if you need to define more than one value, you must use elements. Examples of attributes in Listing 1 are version and encoding in the prefix and id on customer.

    Another important part of XML that you’ll need to understand is namespaces. may be you learned how namespaces in C# and VB help give a unique identity to code within a given namespace. The purpose of namespaces in XML is similar. In the case of Listing 1, there is a customer element, but think about how many different programs work with customer data. A customer in one program will not be defined the same as a customer in another program, and you need a way to tell them apart, which is where namespaces come in. You would define your customer data in a namespace of your choosing, and some other developer would define a unique namespace for their customer. That way, your programs won’t ever be confused if they try to read the wrong data. Listing 2 shows how to use a namespace to make a customer unique.

    Listing 2 XML namespace example

    <?xml version="1.0" encoding="utf-8" ?>
    <customer id="7"
    <a:address>123 4th St</a:address>

    Namespaces are specified by placing an xmlns attribute on an element, either with or without a prefix. The xmlns without a prefix specifies the default namespace for all of the elements where the namespace resides and child elements of the element where the namespace resides. This means that customer and name are in the http://mcgraw-hill.com/ vs2010bg namespace. Namespaces can also have prefixes to help you target where they are applied. In Listing 2, there is an xmlns:a, where a is the prefix for the http://somedomain.com/ addresses namespace. The convenience of prefixes is that they help the XML be more readable. In Listing 2, the address namespace is decorated with the a: prefix, as in <a:address> to indicate that address belongs to the http://somedomain.com/addresses namespace. Without the prefix, you would be forced to write the address element as follows, which is more difficult to read:

    < http://somedomain.com/addresses:address>
    123 4th St
    </ http://somedomain.com/addresses:address>

    I added line breaks for readability, but in practice the only part of the data read is the value and not the white space, such as newlines, surrounding it.

    The XML Menu
    When you open an XML document in VS, you’ll see an XML menu appear with options for running, debugging, and profiling XML Transformation (XSLT) documents and working with schemas. XSLT is used by a running program or utility to change an XML document from one form to another. An XML schema is an XML document that describes the allowable format of another XML document. An XML schema is to an XML document what a SQL table definition is to the data that the table holds. Both XSLT and schemas are outside the scope of this book, but now you know where the tools are in case you need to work with them.

    Configuring XML Options

    Selecting Tools | Options will open the VS Options window. From the Options window, you can select Text Editor XML and configure many options associated with writing XML documents, such as turning on line numbering or specifying tag formatting.

    No comments :

    Post a Comment