Login | Register   
RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX

By submitting your information, you agree that devx.com may send you DevX offers via email, phone and text message, as well as email offers about other products and services that DevX believes may be of interest to you. DevX will process your information in accordance with the Quinstreet Privacy Policy.


Visual Basic and Respect : Page 2

Will VB's new LINQ to XML prowess finally gain it the community respect accorded to other mainstream languages?




Application Security Testing: An Integral Part of DevOps

LINQ to XML Examples
Assume you can create an XML file containing customer information, and you'd like to create a new customer. In C# (or in Visual Basic), you could write the code like this:

[C#] XDocument customers = new XDocument( new XDeclaration("1.0", "utf-8", "yes"), new XComment("Sample customer information"), new XElement("Customers", new XElement("Customer", new XAttribute("CustomerID", "MICRO"), new XElement("Company", "Microsoft"), new XElement("ContactName", "Andrew Fuller"), new XElement("Country", "USA"), new XElement("Orders", new XElement("Order", new XAttribute("OrderID", "10643") ) ) ) ) );

You have to admit, the new System.Xml.Linq.XDocument class (and its friends, XElement, XAttribute, and so on) make creating XML content a lot easier. But it's still a lot of tricky code, requiring careful placement of parentheses and commas. On the other hand, you can create the same fixed content in Visual Basic using embedded XML, like this:

[Visual Basic] Dim customers = _ <?xml version="1.0" encoding="utf-8" standalone="yes"?> <!--Sample customer information--> <Customers> <Customer CustomerID="MICRO"> <Company>Microsoft</Company> <ContactName>Andrew Fuller</ContactName> <Country>USA</Country> <Orders> <Order OrderID="10643"/> </Orders> </Customer> </Customers>

What's more, as you type this code in the Visual Basic editor, the editor creates closing tags for you, and formats the XML as you type.

Suppose you have a list of customers in a collection of XElement instances, and you'd like to display specific elements and attributes. In C#, you might write code like this:

foreach (var item in items) { Console.WriteLine("{0}: {1,-40} ({2})", item.Attribute("CustomerID").Value, item.Element("Company").Value, item.Element("Phone").Value); }

Not only must you call functions (Attribute, Element, and so on) to retrieve the values, you simply pass the name of the item you want to the function, and there's no compile-time checking of your typing. Visual Basic, on the other hand, supports XML literals, which not only allow you to simply refer to values within the XML content, but if you've added a schema for your data to your project, you even get IntelliSense from the schema as you type. I can't show that here, but you can certainly appreciate the neater code (and can imagine the IntelliSense you'd see as you type):

[Visual Basic] Console.WriteLine("{0}: {1,-40} ({2})", _ item.@CustomerID, _ item.<Company>.Value, _ item.<Phone>.Value)

Both Visual Basic and C# allow you to use LINQ to transform XML content, using the query's from and where clauses to the "columns" and "rows" of data to work with, and the select clause allows you to generate new XML content as you iterate through the data from the input XML. For example, the following C# code formats a Customer list so that it becomes a Contacts list:

[C#] XElement contacts = new XElement("Contacts", from c in customers.Elements("Customer") where c.Element("Country").Value == "USA" select new XElement("Contact", new XElement("Name", c.Element("ContactName").Value), c.Element("Company"), new XElement("Phone", new XAttribute("type", "Work"), c.Element("Phone").Value), new XElement("Phone", new XAttribute("type", "Fax"), c.Element("Fax").Value) ) );

The result of running that code, in its context, is XML content that looks like this (with one contact for each customer in the original list):

<Contacts> <Contact> <Name>Liu Wong</Name> <Company>The Cracker Box</Company> <Phone type="Work">(406) 555-5834</Phone> <Phone type="Fax">(406) 555-8083</Phone> </Contact> </Contacts>

Combining Visual Basic's support for embedded XML and XML literals with its support for replacement tokens (runtime-replaced values surrounded in code with ASP.NET's <%= %> delimiters), you can rewrite the previous code like this:

Dim contacts = _ <Contacts> <%= From c In customers.<Customer> _ Select <Contact> <Name><%= c.<ContactName>.Value %></Name> <%= c.<Company> %> <Phone type="Work"> <%= c.<Phone>.Value %></Phone> <Phone type="Fax"> <%= c.<Fax>.Value %></Phone> </Contact> %> </Contacts>

At first glance, it seems complicated because of all the strange tokens, but as you study the code, it becomes clear that this is an elegant way to perform repetitive XML transformations.

Is superiority in the LINQ over XML arena going to convince C# developers to move to Visual Basic? I doubt it. But it does prove that the Visual Basic team is doing some truly interesting and innovative things, and demanding respect for the language. I firmly believe that you should always use the tool that gets you the results you need in the most expedient manner possible-when working with LINQ over XML, that's certainly Visual Basic, as far as I'm concerned. If Visual Basic keeps adding features like these, I'm hoping it can shed that Rodney Dangerfield aura, and get the respect it deserves.

Ken Getz is a senior consultant with MCW Technologies and splits his time between programming, writing, and training. He specializes in tools and applications written in Visual Studio .NET and Visual Basic. Ken is the author of the highly rated .Finalize() column in CoDe Magazine. He is also the co-author of several best-selling books, including Access 2002 Developer's Handbooks with Paul Litwin and Mike Gunderloy, Visual Basic Language Developer's Handbook with Mike Gilbert, and VBA Developer's Handbook with Mike Gilbert (Sybex). He co-wrote several training courses for Application Developer's Training Company, including VB.NET, ASP.NET, Access 2000 and 97, Visual Basic 6, and Visual Basic 5 seminars. He has also recorded video training for AppDev covering VB.NET, ASP.NET, VB6, Access 2000, and Access 97. Ken is a frequent speaker at technical conferences and has spoken often at Microsoft's Tech-Ed conference. Ken's also a technical editor for Access-VB-SQL Advisor magazine and a columnist for Informant Publications' asp.netPRO magazine..
Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.



We have made updates to our Privacy Policy to reflect the implementation of the General Data Protection Regulation.
Thanks for your registration, follow us on our social networks to keep up-to-date