Content by Category
.NET 1.x
.NET 2.0
.NET 3.0
.NET 3.5
.NET 4.0
.NET 4.5
.NET Assemblies
.NET Framework
.NET Getting Started
Accessibility
ADO.NET
Advertorials
Agile Development
AJAX
Amazon Web Services
Analysis Services
Android
Architecture
Arduino
ASP .NET Web API
ASP.NET
ASP.NET MVC
ASP.NET WebForms
Azure
B2B (Business Integration)
BDD
Big Data
Bing
BizTalk
Book Excerpts
Build and Deploy
Business Intelligence
C#
C++
ClickOnce
Cloud Computing
Code Contracts
CODE Framework Info - non Technical
CODE on the Road!
COM+
Community
Conferences
Continuous Integration
Crystal Reports
CSLA.NET
CSS
Data
Debugger
Design Patterns
Development Process
Display Technologies
Distributed Computing
Document Database
DotNetNuke
DSL
Dynamic Languages
Dynamic Programming
Editorials
Enterprise Services ("COM+")
Entity Framework
Events
Expression Blend
F#
Fox to Fox
Frameworks
Functional Programming
Git
Graphics
HTML 5
Internet Explorer 8.0
Interviews
IOS
iPhone
Iron Ruby
Java
Java Script
JavaScript
jQuery
JSON
Lightswitch
LINQ
Linux
LUA
Mac OS X
MDX
Messaging
Metro
Microsoft Application Blocks
Microsoft Business Rules Framework
Microsoft Dynamics
Microsoft Expression
Microsoft Office
Mobile Development
Mobile PC
Mono
MsBuild
MVVM
MySQL
Network
NHibernate
node.js
NOSQL
Nuget
Object Oriented Development
Objective C
Odata
OLAP
Open Source
Opinion
Opinions
Oracle
ORM
Other Languages
Parallel Programming
Patterns
PHP
Podcasts
Post Mortem
PowerPoint
Print/Output
Prism
Product News
Product Reviews
Project Management
Prolog
Python
Q&A
Rails
Rake
Razor
Reporting Services
REST
RIA Services
Ruby
Ruby on Rails
Scheme
Search
Security
Services
SharePoint
SignalR
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 2012
SQL Server CE/AnyWhere/Mobile/Compact
SSIS
Subversion
Sync Framework
Tablet PC
TDD
Team System
Techniques
Testing and Quality Control
TFS
Tips
TypeScript
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 11
Visual Studio 2005
Visual Studio 2008
Visual Studio 2010
Visual Studio 2011
Visual Studio 2012
Visual Studio Tools for Office
VSX
WCF
Web Development (general)
Web Services
WebMatrix
WF
Whitepapers
Windows 7
Windows 8
Windows Azure
Windows Live
Windows Phone 7
Windows Phone SDK
Windows Server
Windows Vista
WinForms
WinRT
Workflow
WPF
XAML
Xiine Documentation
XML
XNA
XSLT



LearnNow


XAMALOT
 


SSWUG

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 Massi is a Senior Program Manager on the Microsoft Visual Studio BizApps team who build the Visual Studio tools for Azure, Office, SharePoint as well as Visual Studio LightSwitch. Beth is a community champion for business application developers and is responsible for producing and managing content and community interaction with the BizApps team. She has over 15 years of industry experience building business applications and is a frequent speaker at various software development events. You can find her on a variety of developer sites including MSDN Developer Centers, Channel 9, and her blog www.BethMassi.com. Follow her on twitter @BethMassi

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

134 people have rated this article.

Instantly Search Terabytes Of Text
“Lightning Fast”
– Redmond Mag
“Covers all data
sources” – eWeek
25+ fielded & full-text search options
dtSearch’s own document filters highlight hits in popular file types
Web Spider supports static & dynamic data
APIs for .NET, Java, C++, SQL, etc.
Win / Linux (64-bit & 32-bit)
www.dtSearch.com
 

      Sharepoint TechCon

 

Sharepoint TechCon