Including XML Directly in Your Code: Using XML Literals

Applications work with many types of data, and Extensible Markup Language (XML) has become one of the more popular formats for data. Visual Basic now lets you use XML markup in your Visual Basic code through XML literals. In this lesson, you will learn how to use XML markup directly in your code, and how you can also embed expressions in the XML.

You can easily create XML elements and XML documents programmatically by using XML literals. For example, if you want to create an XML element, you can declare a variable of type XElement and then assign the variable a block of XML markup.

You can type the XML markup manually, copy it to the Clipboard and paste it directly into the Visual Basic Code Editor, or read the XML from a file. For more information, see How to: Load XML from a File, String, or Stream (Visual Basic). For this lesson, you can copy the XML directly from this topic.

Try It!

To create an XML element

  1. On the File menu, click New Project.

    The New Project dialog box appears.

  2. Click Windows Forms Application and then click OK.

  3. Drag a Button control from the Toolbox to the form.

  4. Double-click the button to enter the default Click event hander in the Code Editor. Add the following code.

    Dim memo As XElement = _
        <memo>
            <date>February 2, 2008</date>
            <to>Patrick Hines</to>
            <from>Roger Harui</from>
            <heading>XML Literals</heading>
            <body>Visual Basic makes XML element creation easy!</body>
        </memo>
    Console.WriteLine(memo)
    
  5. In the Visual Basic IDE, on the View menu, point to Other Windows and then click Output.

    The Output window opens.

  6. Press F5 to run the application.

  7. Click the button, and then close the application and verify that the following XML markup is displayed in the Output window.

    <memo>
        <date>February 2, 2008</date>
        <to>Patrick Hines</to>
        <from>Roger Harui</from>
        <heading>XML Literals</heading>
        <body>Visual Basic makes XML element creation easy!</body>
    </memo>
    

In this code example, the date will always appear as February 2, 2008. If you want to display the current date instead, you can embed an expression inside the XML. For example, you can create a variable for the current date, and then include that variable as an expression by enclosing the variable inside a special tag. The tag starts with an opening angle bracket followed by a percent sign, an equal sign, and then a space. The tag ends with a space and percent sign followed by a closing angle bracket: <%= variable %>.

You do not have to declare the data types for local variables because Visual Basic will infer the type from the value assigned. However, the following code lists the types for clarity. For more information, see Local Type Inference.

To create an XML element that has an embedded expression

  1. Replace the code in the default Click event hander of the button with the following code.

    Dim currentDate As String = Format(Now, "MMMM d, yyyy")
    Dim memo As XElement = _
        <memo>
            <date> <%=currentDate %></date>
            <to>Patrick Hines</to>
            <from>Roger Harui</from>
            <heading>XML Literals</heading>
            <body>Embedding expresions in XML is easy!</body>
            </memo>
    
    Console.WriteLine(memo)
    
  2. Press F5, click the button, and then close the form.

  3. Verify that the XML markup displayed in the Output window displays the current date.

Next Steps

In this lesson, you learned how to use XML literals in your code and how to embed an expression in an XML literal. In the next set of lessons, you will learn about reading from and writing to text files.

Next Lesson: Using the File System: Writing to and Reading from Files

See Also

Concepts

XML Literals Overview

How to: Embed Expressions in XML Literals

Other Resources

Managing Your Records: Using Data in Your Program

LINQ to XML