Content by Category
.NET 1.x
.NET 2.0
.NET 3.0
.NET 3.5
.NET 4.0
.NET Assemblies
.NET Framework
.NET Getting Started
Accessibility
ADO.NET
Advertorials
Agile Development
AJAX
Architecture
ASP.NET
ASP.NET MVC
ASP.NET WebForms
Azure
B2B (Business Integration)
Bing
BizTalk
Book Excerpts
Build and Deploy
C#
C++
ClickOnce
Cloud Computing
Code Contracts
CODE on the Road!
COM+
Community
Conferences
Continuous Integration
Crystal Reports
CSLA.NET
CSS
Data
Design Patterns
Development Process
Display Technologies
Distributed Computing
DotNetNuke
DSL
Dynamic Programming
Editorials
Enterprise Services ("COM+")
Entity Framework
Events
Expression Blend
F#
Fox to Fox
Frameworks
Functional Programming
Git
Graphics
Internet Explorer 8.0
Interviews
iPhone
Iron Ruby
Java
Java Script
jQuery
LINQ
Linux
Mac OS X
MDX
Microsoft Application Blocks
Microsoft Business Rules Framework
Microsoft Dynamics
Microsoft Expression
Microsoft Office
Mobile Development
Mobile PC
Mono
MsBuild
Network
NHibernate
Object Oriented Development
Odata
Open Source
Opinion
Opinions
Oracle
ORM
Other Languages
Parallel Programming
Patterns
Podcasts
Post Mortem
PowerPoint
Print/Output
Prism
Product News
Product Reviews
Project Management
Python
Q&A
Rails
Rake
Reporting Services
REST
RIA Services
Ruby
Ruby on Rails
Search
Security
Services
SharePoint
Silverlight
SOA
Social Networks
Software & Law
Software Business
Source Control
Speech-Enabled Applications
SQL Server
SQL Server 2000
SQL Server 2005
SQL Server 2008
SQL Server CE/AnyWhere/Mobile/Compact
SSIS
Subversion
Sync Framework
Tablet PC
TDD
Team System
Techniques
Testing and Quality Control
Tips
UI Design
UML
User Groups
VB Script
VB.NET
Version Control
VFP and .NET
VFP and SQL Server
Virtual Earth
Vista
Visual Basic
Visual Basic 6 (and older)
Visual FoxPro
Visual Studio .NET
Visual Studio 2005
Visual Studio 2008
Visual Studio 2010
Visual Studio Tools for Office
VSX
WCF
Web Development (general)
Web Services
WF
Whitepapers
Windows 7
Windows Azure
Windows Live
Windows Server
Windows Vista
WinForms
Workflow
WPF
XAML
XML
XNA
XSLT



CODE Training


 


QCon

Reader rating:
Click here to read 16 comments about this article.
Article source: CoDe (2008 Jul/Aug)


Article Pages:  1  2 3 4 - Next >


Sharpening Your Axis with Visual Basic 9

Visual Basic 9 in Visual Studio 2008 has a new set of language features that allows developers to work with XML in a much more productive way using a new API called LINQ to XML. LINQ stands for Language Integrated Query and it allows you to write queries for things like objects, databases, and XML in a standard way. Visual Basic provides deep support for LINQ to XML through what’s called XML literals and XML axis properties. These features allow you to use a familiar, convenient syntax for working with XML in your Visual Basic code. LINQ to XML is a new, in-memory XML programming API specifically designed to leverage the LINQ framework. Even though you can call the LINQ APIs directly, only Visual Basic allows you to declare XML literals and directly access XML axis properties. This article will help you master these new features for working with XML in Visual Basic.

XML Literals and Embedded Expressions

Traditionally to work with XML in code you would use the XML Document Object Model (XML DOM) and use its API to manipulate objects representing the structure of your XML. This API is document centric, which does not lend itself well to creating XML with LINQ. In addition it uses the XPath language, which is unnecessary when using LINQ as you will soon see. Another way to work with XML would be to drop out of Visual Basic code altogether and manipulate XML using XSLT, XQuery, or XPath directly.

With the release of the .NET Framework 3.5, the LINQ to XML API allows developers to work with XML in a much more intuitive way, taking a top-down, element-centric approach to working with XML. Additionally, in Visual Basic 9 you can use XML literals in order to embed XML directly into your code and bring an even more WYSIWYG approach to XML programming. For instance, this is perfectly legal in Visual Basic 9:

Dim myXml = <?xml version="1.0"?>
            <root>
                <node>This is XML</node>
            </root>

The variable is inferred to be an XDocument object in this case. Type inference is another new feature of Visual Basic 9 in order to support LINQ. Types of local variables can be inferred by the compiler by evaluating the right-hand side of the assignment. The above statement in previous versions of Visual Basic would produce a type of Object. With the new Option Infer ON in Visual Basic 9, the statement above is now the same as declaring the XDocument type explicitly so your code still benefits from compile-time checks. XDocument is one of the classes in the new LINQ to XML API and in many cases performs much better than the XML DOM.

As you can see there is no conceptual barrier between the code you write and the XML you’re trying to express when using XML literals in Visual Basic 9. Notice the syntax coloring of the XML in the editor indicating that Visual Basic recognizes this XML literal as a supported data type. As you type the begin tag of the element, Visual Basic will automatically insert the end tag and format the XML. If you change the name of the element begin tag, Visual Basic will update the end tag to match. Also note that underscores are not necessary in multiline XML literals.

XML literals get much more interesting when you use them with embedded expressions. Embedded expressions allow you to write any Visual Basic code and have it evaluated directly in the literal, and you can use any Visual Basic expression. These expressions are compiled code, not script, so you can benefit from the compile-time syntax checking and editor experience just like you’re accustomed to when writing programs in Visual Basic. The syntax you use is <%= expression %>. For instance, taking the simple literal above you could embed an expression in the <node> element in order to write out the current date and time inside the element instead:

Dim myXml = <root>
                <node><%= Now() %></node>
            </root>

Notice this time you did not start with the XML declaration, so instead this is inferred to be an XElement object, the fundamental class in the LINQ to XML API. XML trees are made up of these XElement objects. This is just a simple example; you are not limited to the number of embedded expressions in your XML literals. You can also nest embedded expressions any number of levels deep.

Let’s take an example using a LINQ query that queries the files in the current directory and creates XML using XML literals and embedded expressions.

Dim myXml = _
<files>
   <%= From FileName In _
    My.Computer.FileSystem.GetFiles(CurDir()) _
    Let File = _
    My.Computer.FileSystem.GetFileInfo(FileName) _
    Select _
        <file isReadonly=
            <%= File.IsReadOnly %>>
            <name>
                <%= File.Name %>
            </name>
            <created>
                <%= File.CreationTime %>
            </created>
            <updated>
                <%= File.LastWriteTime %>
            </updated>
            <folder>
                <%= File.DirectoryName %>
            </folder>
        </file> %>
</files>

myXml.Save("MyFiles.xml")

This will produce XML something like this:

<files>
  <file isReadonly="true">
    <name>customers.html</name>
    <created>
      2007-10-07T15:57:00.2072384-08:00
    </created>
    <updated>
      2007-10-22T14:46:48.6157792-07:00
    </updated>
    <folder>C:\temp</folder>
  </file>
  <file isReadonly="false">
    <name>orders.xml</name>
    <created>
      2007-10-07T15:57:00.2773392-08:00
    </created>
    <updated>
      2007-10-27T12:01:00.3549584-07:00
    </updated>
    <folder>C:\temp</folder>
  </file>
  <file isReadonly=”false”>
    <name>plants.xml</name>
    <created>
      2007-10-07T15:57:00.147152-08:00
    </created>
    <updated>
      2007-10-18T18:25:07.2607664-07:00
    </updated>
    <folder>C:\temp</folder>
  </file>
</files>

Notice in the example that you first start writing an XML literal and then embed a LINQ query which selects a collection of XElements <file> in order to create the document. You can nest embedded expressions this way to create very complex documents in a much more intuitive way. This example creates the XML from the top down just as you would read it. This enables you to be much more productive using the LINQ to XML API than its predecessor, the XML DOM, which forced you to think in terms of its API instead of the actual XML you’re trying to express.

&

By: Beth Massi

Beth is an Online Content and Community Program Manager on the Visual Studio Community Team at Microsoft, responsible for producing content for business application developers and driving community features onto MSDN Developer Centers. She also produces content on her blog (http://blogs.msdn.com/bethmassi), the VB Team blog (http://blogs.msdn.com/vbteam) and Channel 9. As a VB community champion and a long-time member of the Microsoft community she also helps with the San Francisco East Bay .NET user group and is a frequent speaker at various software development events. Before Microsoft, she was a Senior Architect at a health care software product company and a Microsoft Solutions Architect MVP. Over the last decade she has worked on distributed applications and frameworks, Web, and Windows-based applications using Microsoft development tools in a variety of businesses. She loves teaching, hiking, mountain biking, and modifying cars.

By: Avner Aharoni

Avner Aharoni is a Senior Program Manager at Microsoft and was the program manager responsible for the XML language integration in Visual Basic 9.0. Prior to that, he worked on XML and object to relational mapping and other XML features in SQL Server. Currently Avner is working on the next version of the Visual Basic language.

avnera@microsoft.com

Fast Facts

Visual Basic 9 completely eliminates the barrier between the code you write and the XML you’re trying to express. Creating, querying, and transforming XML is much more intuitive and productive than ever before.



Article Pages:  1  2 3 4 - Next Page: 'XML Axis Properties' >>

Page 1: Sharpening Your Axis with Visual Basic 9
Page 2: XML Axis Properties
Page 3: Importing XML Namespaces
Page 4: Creating Relational Data from XML
Page 5:
Page 6:
Page 7:

How would you rate the quality of this article?
1 2 3 4 5
Poor      Outstanding

Tell us why you rated the content this way. (optional)

Average rating:
3.7 out of 5

125 people have rated this article.

      Hacker Halted

 

Free Webinar