MSDN Magazine - February 2008 - (Page 25) XML Programming in Visual Basic 9.0 JONATHAN ANEJA B y now you’ve probably heard of LINQ (or Language Integrated Query), the new query technology coming in Visual Studio® 2008. LINQ-enabled languages like Visual Basic® give you a rich set of query operators that can be applied to various data sources, such as in-memory collections, databases, datasets, and XML. That alone is pretty cool, but Visual Basic 9.0 actually goes beyond that and makes XML a first-class data type directly in the language. Now you may be wondering why you would want an XML data type integrated directly into Visual Basic. Today many applications use XML for both storage and data transfer. XML has gained broad adoption across the industry due to its flexibility and simplicity, and it is used in many apps for both storage and data transfer. It works particularly well for transferring data among systems since it is self-describing (meaning that the structure of the data is included with the data). Plus, it’s much easier to read data structured inside XML tags than it is to write parsing logic for various custom file formats. The problem with XML, however, is that it has never been particularly easy for developers to work with. Awkward and inconsistent APIs, such as the Document Object Model (DOM), and languages such as XSLT and XQuery lead to writing a lot of tedious code that is often difficult to read and understand. But with the introduction of LINQ and Visual Basic 9.0, XML development becomes much easier. In this column I will explore the current XML programming experience, how LINQ improves the experience, and how Visual Basic provides even more support when working with XML. To get started, say I need to write out a list of customers in XML. My customer list has the following properties: FirstName, LastName, Address, City, State, and ZipCode. When I convert this to XML, it should look something like Figure 1. Using the DOM and Visual Studio 2005, I could write code that loops through my list and constructs the appropriate XML nodes, as shown in Figure 2. As you can see, that’s a lot of code to write for something that should be pretty simple. It’s also very hard to visualize the structure of the generated XML since it doesn’t really match the structure of the code. In the long term, maintaining this code will pose a huge inconvenience. If business requirements change and I find that I need to add some properties or attributes, I would have to wade through this code and figure out which nodes to update. Figure 1 Customer List Properties in XML 123 Main St Redmond WA 10104 456 First St Seattle WA 10028 Figure 2 Using the DOM to Construct the XML Nodes ‘With Visual Basic 9, you no longer have to write code like this. Public Function ConvertToXML(ByVal custList As List(Of Customer)) _ As String Dim doc As New XmlDocument Dim root As XmlElement = doc.CreateElement(“Customers”) For Each cust As Customer In custList Dim custElement As XmlElement = doc.CreateElement(“Customer”) custElement.SetAttribute(“FirstName”, cust.FirstName) custElement.SetAttribute(“LastName”, cust.LastName) Dim Dim Dim Dim address As XmlElement = MakeElement(doc, “Address”, cust.Address) city As XmlElement = MakeElement(doc, “City”, cust.City) state As XmlElement = MakeElement(doc, “State”, cust.State) zipcode As XmlElement = MakeElement(doc, “ZipCode”, cust.Zip) Using the DOM With custElement .AppendChild(address) .AppendChild(city) .AppendChild(state) .AppendChild(zipcode) End With root.AppendChild(custElement) Next doc.AppendChild(root) Return doc.InnerXml End Function Private Function MakeElement(ByVal doc As XmlDocument, _ ByVal elementName As String, _ ByVal value As String) As XmlElement Dim element As XmlElement = doc.CreateElement(elementName) element.InnerText = value Return element End Function This column is based on a prelease version of Visual Studio 2008. All information herein is subject to change. february2008 25
For optimal viewing of this digital publication, please enable JavaScript and then refresh the page. If you would like to try to load the digital publication without using Flash Player detection, please click here.