11th December 2007, 04:22 pm
I really liked the XML expressiveness of Visual Basic, let’s build a very simple MSN History Search Engine using LINQ and XML Literals in Visual Basic.
The best thing is those literals could be used in LINQ expressions. Remember the simple XML file that MSN stores as a history.
- .@AttributeName : Accesses the attribute element in XML
- .<ElementName> : Accesses the element in XML
- …<Descendant name>: Accesses the descendant name in XML
Modifying the XML content is very neat as well either using the LINQ expressions or even in loops.
In .NET Framework 2.0 VB has one more feature called MY namespaces. It is very nice to access some dynamic data available like application or forms information. It also contains some helper functions to do some common tasks. Now I also found it very handy in a Windows Forms application.
Get the history files from the location and operate the XLINQ query:
Private Sub btnSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
For Each file In My.Computer.FileSystem.GetFiles(dirLocation)
ProcessFile(file)
Next
txtOutput.Text = sBuild.ToString()
End Sub
The XLINQ query that does search magic for the messages is as follows :
Function ProcessFile(ByVal s As String) As Boolean
If s.EndsWith("xml") Then
Dim msn = XElement.Load(s)
Dim q = From message In msn.<message> _
Where message.<text>.Value.Contains(txtSearch.Text) _
Select From = (message.<from>.@FriendlyName), Too = (message.<to>.@FriendlyNam), _
Message = message.<text>.Value
For Each msgFound In q
sBuild.AppendLine(msgFound.From + " says to " + msgFound.Too + _
" : " + msgFound.Message)
Next
End If
End Function
What makes this different is the usage of literals. In C# that query would be longer than that.
In the sample message.<From>.@FriendlyName
means that it will get the From element and get the friendlyname attribute from it.
It is basically like having the XML data in your hands but there is no need to parse it or access the elements using the classes provided rather this work is done by the compiler at the compile time.
In a couple lines of code we have a fully featured MSN history searching. Let me know if you still want the source code (although that is all about it) or even the executable in case you are not into programming.
11th December 2007, 03:34 pm
Here is the summary of new language features as mentioned on the “What’s new on ORCAS
LINQ is all in both of the languages and indeed this is the main feature for .NET Framework 3.5. Writing any type of queries is the purpose of LINQ at the end. Considering the abilities of LINQ, everything was possible before as well. LINQ makes us to get rid of the strings (the red coloured stuff) from the program in order to minimise the typo errors, easy to read programs by syntax highlighting. But all the best is that gives the ability to write declarative and functional style programs.
Beside the new language features, as a compiler improvement, it is very surprising that C# still doesn’t have background compilation. There is background syntax checking but no compilation. I believe this is a definite need for C# because it is really helpful. For instance the F# projects do background compilation and syntax checks, that way it easy to investigate “silly errors” while coding. Also this was one of the powerful features that I found on eclipse while working on a Java project.
Visual basic has that feature moreover it has also automatic syntax fixing as well. Likewise if you call a method with lower case letters it is automatically converted to the actual method on the next line. Actually in a type inferenced language this is needed, because it is not easy to recognise the type information of all the members.
Anyway I just wrote a quick macro to give the feeling of background compilation for C#. It is not really sophisticated but it works. Just put it into EnvironmentEvent macro in Visual Stuio.
Dim lastbuilt As DateTime
Private Sub TextDocumentKeyPressEvents_AfterKeyPress(ByVal Keypress As String, ByVal Selection As EnvDTE.TextSelection, ByVal InStatementCompletion As Boolean) Handles TextDocumentKeyPressEvents.AfterKeyPress
Dim doc = DTE.ActiveDocument
Dim diff = DateTime.Now.Subtract(lastbuilt)
If Not Char.IsLetterOrDigit(Keypress(0)) And diff.Seconds > 5 Then
DTE.ExecuteCommand("Build.BuildSelection")
lastbuilt = DateTime.Now
doc.Activate()
End If
End Sub
XML in Language
All of the best is that now XML is a first class citizen in VB. I wouldn’t expect this as a serious feature but after trials it makes extremely relevant to use XML in Visual Basic. You get syntax highlighting and even intellisense for XML if the namespaces are specified and even more.
Having XML literals in the language, it makes really sense to use XLinq with VB.
Like consider the xml stored by messenger. You could just assign to a variable just like that.
Dim msn = <?xml version="1.0"?>
<?xml-stylesheet type='text/xsl' href='MessageLog.xsl'?>
<Log FirstSessionID="1" LastSessionID="1">
<Message Date="25/03/2007" Time="22:35:47" DateTime="2007-03-25T21:35:47.173Z" SessionID="1">
<From><User FriendlyName="koko"/></From>
<To><User FriendlyName="opopop"/></To>
<Text Style="font-family:Comic Sans MS; font-weight:bold; color:#0000a0; ">151515</Text>
</Message>
<Message Date="25/03/2007" Time="22:35:55" DateTime="2007-03-25T21:35:55.344Z" SessionID="1">
<From><User FriendlyName="koko"/></From>
<To><User FriendlyName="opopop"/></To>
<Text Style="font-family:Comic Sans MS; font-weight:bold; color:#0000a0; ">5959959</Text></Message>
</Log>
It will have the type of System.Xml.Linq.XDocument.
Let’s define the XML Stylesheet :
Dim xslt = <?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="Log">
<html>
<head>
<title> Message Log for </title>
</head>
<body style='margin:0'>
<table border='1'>
<tr>
<td> From </td>
<td> To </td>
<td> Message </td>
</tr>
<xsl:for-each select="/Log/Message">
<tr>
<td><xsl:value-of select="From/User/@FriendlyName"/></td>
<td><xsl:value-of select="To/User/@FriendlyName"/></td>
<td><xsl:value-of select="Text"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
If we want to do an XSLT transformation to that snippet, it is even easier than it used to be.
Dim xTransform = New System.Xml.Xsl.XslCompiledTransform()
xTransform.Load(xslt.CreateReader())
xTransform.Transform(msn.CreateReader(), New System.Xml.XmlTextWriter("test.html", New System.Text.UnicodeEncoding()))
I think working with xml data using Visual Basic should be considered as a manipulation language. Since we are all becoming multilingual this shouldn’t be a problem.
Tags:
cs,
csharp,
linq,
macro,
vb,
xlinq,
XML,
xsl,
xslt Category:
.Net,
Articles,
C#,
Visual Basic,
XML,
linq |
Comment
12th September 2007, 08:29 pm
Although I am extremely busy this month, I can’t stop reading those wonderful articles about parallelism on October’s MSDN Magazine. Here are the articles, a very first gentle introduction to PLINQ (Parallel LINQ) by Joe Duffy,Ed Essey, Parallel Performance using System.Concurrency namespace and a ThreadPool article. Apparently in the near future we will have some more concurrency dlls.
Going back to work, enjoy!
7th August 2007, 01:57 am
It’s getting even more interesting. With the efforts of LINQ to be the solution for entity relationship now Microsoft adds additional features with Astoria services. Although it looks like an independent product, it has a dependency with ADO.Net Entity Framework (System.Data.Entity) which is not included in ORCAS Beta 2 and also not available for beta 2. So I couldn’t play with it.
Astoria as announced is a database service that supports different protocols and standards. At first it looks like Amazon S3 web service, but you can also host it yourself. Why would you want to host it? I don’t think this model would be useful for .NET applications, although you can, why to use another layer. Presumably it’s for Javascript and Silverlight usage mainly, you don’t need any additional libraries, it’s all there as a REST web service. Currently 100MB is provided by Astoria site…
I wonder if the web is going to provide enterprise services sooner or later. Although we store all our e-mail, calendar in the web, I still use my hosting database server or local cache rather than web services. Anyway I like the web relational database idea… I hope to play in the next release.
Tags:
100mb,
amazon,
astoria,
database_service,
entity_relationship,
linq,
orcas,
relational_database,
s3_web,
Silverlight,
Software,
Uncategorized,
web_service Category:
Silverlight,
Software,
Uncategorized |
Comment