Working with XML Directly Within Your Code (VB Only)

You can embed XML directly within your Visual Basic code. This can make creating XML messages and executing queries against XML a simple task in Visual Basic. To support this feature, Visual Basic enables you to write straight XML when using the data types called System.Xml.Linq.XElement and System.Xml.Linq.XDocument. The former enables you to create a variable and assign it an XML element. The latter, XDocument, is used to assign a variable to a full XML document.

Writing XML within your Visual Basic code is a structured process and not just simple strings assigned to a parsing engine. In fact, the compiler uses LINQ to XML behind the scenes to make all this work. Let’s look at a simple example. The following code creates a variable emp of type XElement. It then assigns the XML fragment to this variable.

VB

Dim emp As XElement = <employee>
                          <firstName>Joe Smith</firstName>
                          <title>Sr. Developer</title>
                          <company>Contoso</company>
                          <location>Redmond, WA</location>
                      </employee>

You can create a similar fragment to an XDocument. You simply add the XML document definition (<?xml version="1.0"?>) to the header of the XML. In either scenario, you end up with XML that can be manipulated, passed as a message, queried, and more.

Visual Basic enables you to write XML inside your code. The two objects (XElement and XDocument) are still important to C# developers. However, C# developers work with the properties and methods of these objects directly and do not rely on the editor to parse XML directly within a code window. The following shows the same code sample written using C#. (You need the using statement using System.Xml.Linq;.)

C#

XElement xmlTree1 = new XElement("employee",
    new XElement("firstName", "Joe Smith"),
    new XElement("title", "Sr. Developer"),
    new XElement("company", "Contoso"),
    new XElement("location", "Redmond, WA")
);

In most scenarios, however, you do not want to hard-code your XML messages in your code. You might define the XML structure there, but the data comes from other sources (variables, databases, and so on). Thankfully, Visual Basic also supports building the XML using expressions. To do so, you use an ASP-style syntax, as in <%= expression %>. In this case, you indicate to the compiler that you want to evaluate an expression and assign it to the XML. For XML messages with repeating data, you can even define a loop in your expressions. For example, let’s look at building the previous XML using this syntax. Suppose that you have an object e that represents an employee. In this case, you might write your XElement assignment as shown here.

VB

Dim e As Employee = New Employee()
Dim emp As XElement = <employee>
                          <firstName><%= e.FirstName %></firstName>
                          <lastName><%= e.LastName %></lastName>
                          <title><%= e.Title %></title>
                          <company><%= e.Company %></company>
                          <location state=<%= e.Location.State %>>
                              <%= e.Location.City %>
                          </location>
                      </employee>

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset