Archive for the ‘XML’ Category.

nBloglines .NET Module for Bloglines Web Services

Bloglines is still the only reader that provides an “official API” to make use of their services. I was working on a syndication application and decided to share the .NET Bloglines library that I wrote to use their web services.

Requirements

  • .NET 3.5 Framework

Quick Reference

  • public Bloglines(string username, string password)
        Member of CodingDay.Bloglines

Default constructor, password is optional for count function.

  • public int Count()
        Member of CodingDay.Bloglines

Returns number of unread items from your subscriptions

  • public System.Collections.Generic.IEnumerable <Subscription> GetSubscriptionList()
        Member of CodingDay.Bloglines

Gets the Subscription list from your account

  • public System.Collections.Generic.IEnumerable<Channel> GetChannels(string SubId, bool markasRead)
        Member of CodingDay.Bloglines

Get the rss feeds for a subscription ID returned from the subscription list function. MarkasRead parameter is the synchronization parameter to mark the changes on bloglines servers.

Download


Usage

  • Create CodingDay.Bloglines object

var blines = new CodingDay.Bloglines("***@****.***", "******");

  • Count unread items for the user

 Console.WriteLine("{0}", blines.Count());

  • Get list of subscriptions

 var subscriptions =  blines.GetSubscriptionList();
foreach (var item in subscriptions)
{
  Console.WriteLine("id:{0}, url:{1}, parent:{2}", item.Id, item.Url, item.ParentId);
}

  • Get items for a subscription feed or folder using the id returned from subscription function

// use a number from the subscriptions id
var channels = blines.GetChannels("64363260", false); 
foreach (var item in channels)
{
  Console.WriteLine("title:{0}, description:{1}", item.Title, item.Description);
  foreach (var i in item.Items)
  {
     Console.WriteLine("{0}", i.Description);
  }
}

 

For more details, check out http://www.bloglines.com/services/api/

RSS Feed Parser in 20 Lines with .NET LINQ

RSS is becoming more and more the face of web. I rarely visit the sites rather visit a bunch of them using an RSS reader. I was working with RSS data and realized how LINQ to XML made easier, elegant  and terse to manipulate XML data. Here is a very naive RSS  parser from RSS 2.0 specification

The Subscription and Channel classes are shown below. Normally I prototyped the parser with anonymous types but when using with methods I needed to get rid of the anonymous types and make them typed.

 
 
 
 
 public class Channel
        {
            public string Title { get; set; }
            public string Link { get; set; }
            public string Description { get; set; }
            public IEnumerable<Item> Items { get; set; }
        }
 public class Item
        {
            public string Title { get; set; }
            public string Link { get; set; }
            public string Description { get; set; }
            public string Guid { get; set; }
        }

Notice the usage of automatic properties as well to optimise readability and simplicity of the code.

Actually, here is where the F# power comes from, if I had implemented this in F#, I wouldn’t need to create those mock objects because of the type inference. C# inferenced types still needs to be more manually inferenced, like the subscription and channel class case.

In order to use we need to create an XDocument object that is preferably an RSS feed (that’s the purpose actually :) ) XDocument is an object that take the best features of XMLDocument and XMLReader. It doesn’t load the complete stream, it is possible to read forward and backward. It is very flexible, we can do transformation, parsing, writing, reading, querying etc.

The LINQ parser query is below :

static IEnumerable<Channel> getChannelQuery(XDocument xdoc)
        {
            return from channels in xdoc.Descendants("channel")
                        select new Channel
                        {
                            Title = channels.Element("title") != null ? channels.Element("title").Value : "",
                            Link = channels.Element("link") != null ? channels.Element("link").Value : "",
                            Description = channels.Element("description") != null ? channels.Element("description").Value : "",
                            Items = from items in channels.Descendants("item")
                                    select new Item
                                    {
                                        Title = items.Element("title") != null ? items.Element("title").Value : "",
                                        Link = items.Element("link") != null ? items.Element("link").Value : "",
                                        Description = items.Element("description") != null ? items.Element("description").Value : "",
                                        Guid = (items.Element("guid") != null ? items.Element("guid").Value : "")
                                    }};
        }

At the end there is no magic, it is like opening an XML reader and choosing the element and attribute we like. However LINQ to XML does this so nicely that I don’t wanna use XMLReader anymore.

It is needed to create the XDocument object and pass it to the function. Bare in mind the parsing is not executed until we loop for each element of the list (laziness).

 static void Main(string[] args)
        {
 
            string feedUri = "http://feeds.feedburner.com/canerten";             
            var myFeed = getChannelQuery(XDocument.Load(new StreamReader(HttpWebRequest.Create(feedUri).GetResponse().GetResponseStream())));
 
            foreach (var item in myFeed)
            {
                Console.WriteLine("{0} - {1}", item.Title, item.Description);
 
                foreach (var i in item.Items)
                {
                    Console.WriteLine("{0}", i.Title);
                }
            }
        }

Coding Day – Adventures in Computing – Can Erten’s Blog
Book Review-Expert F#
Distributed Functional Programming with F# MPI Tools
Power of Functional Programming, its Features and its Future

The code output is shown below. You can also get program.cs file below.

What I like about LINQ is its laziness and really gives a unified data model for all different sources. At the end everything is an IEnumerable no matter whether it is a database query or a memory object.

LINQ and XLINQ with Visual Basic Literals

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.

C# 3.0 vs. VB 9.0 and XML in the Language

Here is the summary of new language features as mentioned on the “What’s new on ORCAS

  • C# 3.0 Language Support: This CTP implements all of the C#3.0 language features from the May LINQ CTP including:
    • Query Expressions
    • Object and Collection Initializers
    • Extension Methods
    • Local Variable Type Inference and Anonymous Types
    • Lambdas bound to Delegates and Expression trees
    • Complete design-time support: Intellisense, Formatting, Colorization

  • VB 9.0 Language Support: This CTP contains the following language features:
    • Query Expressions: Basic querying, filtering, and ordering support
    • Object Initializers
    • Extension Methods
    • Local Variable Type Inference
    • Anonymous Types
    • XML literals
    • XML properties
    • New Line and Expression IntelliSense

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.

XML Notepad 2.0

Friday surprise from Microsoft, XML Notepad has rewritten in .net framework 2.0 from C++. It seems that the team did a great job. And best part is it will be available on Codeplex as a  community project.

Here are the features that I liked at first view

  • Open source
  • Comes with source code, samples (look at the directory you’ve installed)
  • Very simple user interface
  • Intellisense support based on XSD.
  • Great navigation and adding of XML objects (like element, attribute, comment) simply by providing before and after points.
  • Copy cut paste XML objects
  • Find feature is also nice, it is possible to use regular expressions or Xpath language.
  • XSL Output after the XSLT transformation.
  • Error list similar façon of Visual Studio

You can try and download XML Notepad 2.0

XMLNotepad

XML Class Generator for C# using XSD for deserialization

Let’s say we have an XML file and we want to deserialize that file to our implemented class. This is an easy task if the XML file is simple. However if it has more complex types, it can take a long time to implement the class without error. XSD comes with .Net framework SDK. I does not have a user interface, we can access it from the command line tools.

  1. We start it from Visual Studio 2005 -> Visual Studio Tools -> Visual Studio Command Prompt .
  2. Next we need to have a valid XML file that I want to generate the class from. I just use for this sample, the xml output of the yahoo search REST query. Just dowload the xml output of the query
    Yahoo Search xml+class+generator or any other xml file that you want to generate the class from. We save the file as xml.
  3. We use the command xsd to the xml file to generate the xsd schema file.
  4. D:\\test>xsd webSearch.xml
    Microsoft (R) Xml Schemas/DataTypes support utility
    [Microsoft (R) .NET Framework, Version 2.0.50727.42]
    Copyright (C) Microsoft Corporation. All rights reserved.
    Writing file ‘D:\\test\\webSearch.xsd’.
    D:\\test>

  5. Next we use the generated xsd file to generate our class. The generated xsd can contain multiple class, so it would be better to use /classes switch. The default language is C#; however you might want to use it in your Visual Basic Project, to do that just add the switch /language:vb
  6. D:\\test>xsd webSearch.xsd /CLASSES /language:vb
    Microsoft (R) Xml Schemas/DataTypes support utility
    [Microsoft (R) .NET Framework, Version 2.0.50727.42]
    Copyright (C) Microsoft Corporation. All rights reserved.
    Writing file ‘D:\\test\\webSearch.vb’.
    D:\\test>xsd webSearch.xsd /CLASSES
    Microsoft (R) Xml Schemas/DataTypes support utility
    [Microsoft (R) .NET Framework, Version 2.0.50727.42]
    Copyright (C) Microsoft Corporation. All rights reserved.
    Writing file ‘D:\\test\\webSearch.cs’.

  7. Now we have the class file that we use to deserialize the xml object without any exception. So I add the XML file and the generated cs file to my project.
  8. using System;
    using System.Collections.Generic;
    using System.Text;
     
    namespace XSDTest
    {
        class Program
        {
            static void Main(string[] args)
            {
                System.IO.StreamReader str = new System.IO.StreamReader("webSearch.xml");
                System.Xml.Serialization.XmlSerializer xSerializer = new System.Xml.Serialization.XmlSerializer(typeof(ResultSet));
                ResultSet res = (ResultSet) xSerializer.Deserialize(str);
                foreach (ResultSetResult r in res.Result)
                {
                    Console.WriteLine(r.Title);
                    Console.WriteLine(r.Summary);
                    Console.WriteLine();
                }
                str.Close();
     
                Console.ReadLine();
            }
        }
    }

  9. Here is the output :
  10. XML Class Generator for C++
    Oracle9i XML Developer’s Kits Guide – XDK. Release 2 (9.2) Part Number A96621-01
    . 19. XML Class Generator for C++ This chapter contains the following sections:
    Accessing XML C++ Class Generator … Accessing XML C++ Class Generator. The XML
    C++ Class Generator is provided with Oracle9i and is also available for XML Class Generator for Java
    Oracle9i XML Developer’s Kits Guide – XDK. Release 2 (9.2) Part Number A96621-01
    . 7. XML Class Generator for Java. This chapter contains the following sections:
    Accessing XML Class Generator for Java … The Oracle XML Class Generator for Java is provided with Oracle9i’s XDK for Java …

As you may see this is the easiest and exceptionless solution for using xml output of some web services. What we simply do is generate the classfile, deserialize the xml file to our class and use it.

Download

.