MSDN Magazine - February 2008 - (Page 28) tion of type List(Of T). This is where you can really see the beauty of LINQ—I can just as easily use the same query operators that I used for collections against databases and other XML documents without having to learn a new API for each. And you can see how I could take data from a SQL Server® database and easily transform it to XML using just a few lines of code. Applications often need to examine XML (perhaps provided through a file or an RSS feed) and make decisions based on the data. This is frequently done through XSL transformations, but LINQ to XML makes this unnecessary. In the example shown in Figure 6, I load in an XML document, filter by customers who live in Seattle, and then select the first and last name. After that, I loop through my query and display all the matching results on screen. At first glance, you might think something seems wrong here. If the compiler infers doc to be of type XDocument (and cust to be of type XElement), how am I able to type things like cust. and cust.@FirstName? XElement obviously doesn’t have these properties, as they’re specific to the XML that I loaded. What’s actually happening here is the Visual Basic compiler has saved me a fair amount of work through a feature called XML properties. XML properties, sometimes referred to as XML axis properties, provide an easy way to retrieve values stored in XML. In the query shown in Figure 6, I actually use all three of the available axes: descendants, elements, and attributes. In XML, a descendant is an element that is nested one or more levels below the current element. The expression “doc ” uses the descendants axis, and it basically says “find all descendants named Customer.” Remember that the XML I have been using throughout has a root node called Customers, and it contains Customer elements. The triple-dot syntax is essentially translated to doc.Descendants(“Customer”). Now I am going to take a look at the cust. .Value expression. In this case, I am using the elements axis, so the line is translated into cust.Elements(“City”).Value. You may be wondering why we need to explicitly call .Value before doing the comparison here (which we don’t need to do for attributes). The reason is that elements such as City can contain other elements, so the expression cust. actually returns an IEnumerable(Of XElement). The .Value extension property concatenates all values stored in that element, which in this case is just a simple text value. The last expression, cust.@FirstName, uses the attributes axis. This is translated to cust.Attributes(“FirstName”) and returns the value stored in the attribute. Since attributes can’t contain elements, there’s no need to call .Value. I then use the Select method and combine the two fields into one, called Name, in order to make things simpler for display purposes. Figure 5 Using XML Literals and Embedded Expressions Public Function ConvertToXML(ByVal custList As List(Of Customer)) _ As String Dim doc = <%= From cust In custList _ Select <Customer FirstName= LastName= > %> Return doc.ToString End Function XML Properties Figure 6 Sample Query Using XML Axis Properties Public Sub DisplaySeattleCustomers() Dim filePath = My.Application.Info.DirectoryPath & “\customers.xml” Dim doc = XDocument.Load(filePath) Dim query = From cust In doc _ Where cust. .Value = “Seattle” _ Select Name = cust.@FirstName & “ “ & cust.@LastName For Each name In query MsgBox(name) Next End Sub piler know the structure of the XML document? The answer is through XSD schemas. If you have a schema for your XML, you can use the Imports statement in Visual Basic to bring your schema into scope. Once you do that, you get real IntelliSense based on your schema. The XML to Schema Tool for Visual Basic 2008 (which you can download at go.microsoft.com/fwlink/?LinkId=102501) allows you to quickly create XSD schemas based on existing XML files. You just point it at an XML file or Web URL, and it then does the grunt work of generating an XSD file. Wrapping Up Visual Basic is all about productivity. And with business applications increasingly depending on XML for things like storage, data transfer, and even user interfaces, it’s important that Visual Basic incorporate features that make it easier for developers to work with XML. Visual Basic 9.0 steps up in a big way, offering rich XML support and powerful new query capabilities. The simple yet powerful query syntax offered by LINQ enables you to write queries against XML just as easily as you would against a database or an in-memory collection. And once you start to play with Visual Basic 9.0, you will discover many new XML features that I haven’t had a chance to touch upon here, including built-in support for XML namespaces, comments, and fragments. Send your questions and comments to instinct@microsoft.com. Jonathan Aneja is a Program Manager on the Visual Basic team at Microsoft. He works mainly on compiler features, such as LINQ, as well as on other projects, such as the Interop Forms Toolkit. Jonathan has been at Microsoft for about two years, where he started out in ISV Advisory Services. XML Schema IntelliSense It would have been great if IntelliSense® would have told me the name of the fields in my XML document while I was coding up that last example. The problem, however, is how can the com28 msdnmagazine Basic Instincts http://go.microsoft.com/fwlink/?LinkId=102501
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.