Visual Basic .NET 2010 Express - Using XML

106 22
< Continued from page 4

LINQ - Language INtegrated Query - was introduced in Framework 3.5 in November, 2007. Before LINQ, you added statements to your program that used various different data technologies like SQL or XML. We saw how to use XML without LINQ on the previous page. If you were using SQL, you had to actually use a SQL statement in your program something like this:

Dim strSQL as String = _"SELECT Name, CustomerID FROM Customers"


With LINQ, this statment is actually part of the VB.NET language (that is, "Language INtegrated"). It uses a "SQL-like" syntax, but it's not SQL and you can use it for a lot of different kinds of data.

Dim customers = From cust In CustomersSelect cust.Name, cust.CustomerID
Because LINQ lets you get your data directly using statements native to Visual Basic, the data is returned as strongly-typed objects (An integer really is an integer. Databases sometimes use data types that .NET doesn't use and this can cause real problems.) You also get the benefits of IntelliSense when your data access statements are part of the language for faster coding. The data objects returned by LINQ can be used in new queries somewhere else in your program just like they were the original source and you can bind these LINQ objects directly to controls. These are things that you just couldn't do with data before LINQ.

Before coding the LINQ version of the Signature Block program, here's an example using LINQ to embed XML data directly in your actual program code.

(You don't need to understand everything about this example yet, but you can probably guess how it works just by studying the example for a few minutes even if you have never seen XML before.)

Imports <xmlns:ns="http://www.visualbasic.D106/">Module XMLSample Sub myXML()Dim customer = _ <ns:customer><ns:firstName>George</ns:firstName><ns:lastName>Washington</ns:lastName> </ns:customer> End SubEnd Module
Microsoft designed LINQ to work with a variety of data sources such as SQL databases too. The idea is to add a whole new way to get data into your program that will work now and in the future. Microsoft has made it easy for third party software vendors to extend LINQ with their own LINQ services to ensure that it will be used as widely as possible.

You may have noticed that the code to get the values from the XML elements earlier didn't seem too efficient. In a way, it was a bit like the old sequential files. This is because the DOM does everything from the point of view of the entire document. To get the "FName" element, it was necessary to read the entire document, right down to the angle brackets, and then use a test (I used Select Case) until the right element comes up.

LINQ to XML uses a much different concept. If you want the "FName" element, it lets you get the "FName" element. So rather than ...

Dim myNode As XmlNodeDim myXmlNodeReader As XmlNodeReadermySigBlock.Load(SigDocument)myNode = mySigBlock.DocumentElement.SelectSingleNode("SigBlock")myXmlNodeReader = New XmlNodeReader(myNode)Do While (myXmlNodeReader.Read())If myXmlNodeReader.NodeType = XmlNodeType.Element ThenSelect Case myXmlNodeReader.NameCase "FName"... etc
... you can go right to the element ...

Dim mySigBlockXML As XElement = XElement.Load(SigDocument)Dim myFName = mySigBlockXML...<FName>.Value
(Special note: If you're upgrading a project from an earlier version of VB, make sure Option Infer is set to On. See Option Infer - Round Three for details.)

The reason DOM coding is so much more difficult is that you have to reference the XML completely within the context of the document. LINQ lets you refer to the content that you're interested in without starting at the beginning.

The LINQ code above gives you the first (in this case, the 'only') FName element. If you want to get an IEnumerable collection of all of them, you can use this code:

Dim myFName = From FName In mySigBlockXML...<SigBlock> Select FName.<FName>.ValueFor Each FNameVal In myFNameDebug.WriteLine(FNameVal)Next
LINQ also lets you use VB code as part of an "in memory" XML document or document fragment (called an XElement in LINQ) with what are called "embedded expressions". The syntax is <%= expression %>. You might recognize this as the same syntax used in ASP.NET. So, for example, if you wanted to construct a new XML document with content from the existing one, you could code ...

Dim SigSSNs = _<SSN><%= From SSN In mySigBlockXML...<SigBlock> _Select SSN.<SSN> %></SSN>
This creates the XElement type ...

<SSN><SSN>123-45-6789</SSN></SSN>
The program works much better now. For this version, some unnecessary code and one of the buttons has been eliminated while the new LINQ coding was added. The word programmers use for this kind of optomization is "refactoring". You can download the LINQ version here. But it really should do more. One thing that might be nice would be to store more than one signature block and allow the user to select the one that they want. We'll see how to use XML to do that in the next version.

But before we get to that, both sequential logic and event-driven logic are used in all of the example programs we've done in this tutorial, but there's a difference that you have to be completely clear about to write your own VB Express programs. Since it's critical to understand event-driven programming, we cover that next in part 9, Programming Logic and System Architecture!
Source...
Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.