Geeks With Blogs
Imran Shaik Silverlight Quintessential Rambling

In our previous tutorial we have seen how we can use XML axis properties to consume XML returned from a web service in Silverlight applications, in this example however we will look at how XML Literals and Embedded Expressions can be used to easily write fast and efficient Web Services.

*Note XML Literals and Embedded Expression are Visual Basic 9 Exclusive features you might need Visual Studio 2008 and .NET Framework 3.5 installed.

XML Literals and Embedded Expression are not limited to Silverlight/WPF you can use it where ever you want as long as you are using Visual Basic 9 with Visual Studio 2008.

XML Literals

So, what is XML Literals? XML literals is a nice and clean way to write XML in Visual Basic, to use XML Literals "option infer" should be set "on", before we look at XML Literals, a little about literals, what are literals, for any VB.NET programmer the following code should be very familiar.

Dim Product = "Visual Basic"
Dim Version = 9
Dim SubVersion = 9.0
Dim ThisTime = #3/30/2008 3:02:00 PM#
Dim Tomarrow = #3/31/2008#

Here, I am not declaring the type of the variable, but literals allow VB to understand that Product is a string,  version is integer, subversion is double, Thistime is DataTime and Tomarrow is Date, the values that I have provided are called literals, now it should be easy to understand when I say VB 9 supports XML Literals. So instead of using quotes and escape quotes to construct XML we can simple use XML literals and just start writing XML, the following is absolutely legitimate.

Dim Products = _
<VisualStudio Version="2008">
        <Language Name="Visual Basic" Version="9"/>
        <Language Name="C#" Version="3"/>

Yes, no more escape quote to construct XML, just start writing it there and then.

Embedded Expressions

Now, you might argue that we don't use XML inside the programs, we just need a clean way to manipulate XML not define them in our programs, but I never said we are finished with it, there is more.

In our previous example we used XDocuments/XElement/XAttribute to write XML in our web service, which can also be used to write XML in C#

This is what we had,

<WebMethod()> _
   Public Function AllCountries() As String

       Dim sud As New CountryDataSetTableAdapters.CountryTableTableAdapter

       Dim XDataSet As New CountryDataSet.CountryTableDataTable


       Dim MyNameSpace As XNamespace = ""

       Dim XDoc As XDocument = New XDocument(New XDeclaration("1.0", "utf-8", "yes"), _
                                             New XElement(MyNameSpace + "Countries", _
                                                       From country In XDataSet _
                                                       Select New XElement("Country", _
                                                                            New XAttribute("Code", country.CountryISO), _
                                                                           New XAttribute("Name", country.CountryName))))

       Return XDoc.ToString
   End Function

But with Embedded Expressions and XML Literals we can make it even easier to write XML, but first what is embedded expressions, we have seen what XML literals is before and it is not very practical because we don't use XML in our program, but embedded expressions extend the user of XML Literals, embedded expressions are used to embedded values where required using <%= %> syntax.

So the previous WebMethod can be re-written using XML Literals and Embedded Expressions, but first let us find out what we are trying to write, this is the format of the XML we want to construct.

<Countries xmlns="">
    <Country Code="" Name=""/>

So we can replace our previous Web Service using XML Literals and embedded expression using something like,

    <WebMethod()> _
    Public Function AllCountriesUsingXMLLiterals() As String

        Dim sud As New CountryDataSetTableAdapters.CountryTableTableAdapter

        Dim XDataSet As New CountryDataSet.CountryTableDataTable


        Dim XDoc = _
            <Countries xmlns="">
                <%= From country In XDataSet Select <Country Code=<%= country.CountryISO %> Name=<%= country.CountryName %>/> %>

        Return XDoc.ToString
    End Function

Thats it! Working with XML have never been so easy, and it can't get easier than this. Enjoy the good features of VB 9. More examples coming soon.

Posted on Sunday, March 30, 2008 6:45 AM Silverlight , WPF | Back to top

Comments on this post: XML Literals and Embedded Expressions

No comments posted yet.
Your comment:
 (will show your gravatar)

Copyright © Imran Shaik | Powered by: