Merging XML using Visual Basic 9

Merging XML using Visual Basic 9

  • Comments 8

I made some modifications to my Visual Studio Tip of the Day browser application I created last week. There I am downloading the RSS feed from Sara's blog and saving it to disk so that I only retrieve the next tip every 24 hours. However, the RSS only returns the last 15 posts so instead of completely replacing the cached feed on disk I want to merge the old tips from the cache file into the downloaded rss feed so that the older tips are preserved. This is really easy using a LINQ to XML query. 

If My.Settings.CacheFile = "" Then

            My.Settings.CacheFile = _

       Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\tiprss.xml"

End If

 

Dim file = My.Computer.FileSystem.GetFileInfo(My.Settings.CacheFile)

'Only load from the web once per day, else load from the cache

If Not file.Exists OrElse file.LastWriteTime.Date < Now.Date Then

 

      Me.Feed = XDocument.Load(My.Settings.TipsURI)

 

      If file.Exists Then

          'Put the old items from the cache into the new feed.

          Dim cache = XDocument.Load(My.Settings.CacheFile)

 

          Dim oldItems = From cacheItem In cache...<item> _

                         Group Join item In Me.Feed...<item> _

                             On cacheItem.<guid>.Value Equals item.<guid>.Value _

                             Into Count() _

                         Where Count = 0 _

                         Select cacheItem

 

          Dim items = Me.Feed...<item>

          Dim lastitem = items(items.Count - 1)

          lastitem.AddAfterSelf(oldItems)

      End If

 

      Me.Feed.Save(My.Settings.CacheFile)

Else

      Me.Feed = XDocument.Load(My.Settings.CacheFile)

End If

Notice that all I'm doing here is selecting the old tips from the cache feed by using a group join to the new feed where the count of items in the group is 0. This is similar to the syntax NOT IN(subquery) in T-Sql. This will return any items in the cache feed that do not already exist in the new feed. (I also updated the location of the cache based on community feedback so it would be Vista friendly.) I updated the sample here.

Enjoy!

Leave a Comment
  • Please add 6 and 2 and type the answer here:
  • Post
  • Great sample beth!

  • Hi Beth

    Do you really read ALL comments? It seems you haven't seen mine because if you do, you would said something about it.

    Please say something about the "radiobutton binding" comment I left for you.

    Thanks

    Phil

  • Hi Phil,

    Yes I do read all comments. In addition I do try really hard to get to the hunderds of emails I get per month from customers. I just have not had the time to spend investigating the best approach to radio button binding as it is manual process. It's on my list. If you'd like quick support I'd suggest hitting the forums: http://forums.microsoft.com/msdn/default.aspx?forumgroupid=10&siteid=1

    Cheers,

    -B

  • Today the VS Ecosystem team released the Visual Studio 2008 SDK 1.0 and the Visual Studio 2008 shell

  • Today the VS Ecosystem team released the Visual Studio 2008 SDK 1.0 and the Visual Studio 2008 shell

  • I really like your blog. The kind of practically tips which can be used in daily business. I'm using c# and I miss this way of thinking in c# blogs.

  • Yesterday the VS Ecosystem team released the Visual Studio 2008 SDK 1.0 and the Visual Studio 2008 shell

  • Yesterday the VS Ecosystem team released the Visual Studio 2008 SDK 1.0 and the Visual Studio 2008 shell

Page 1 of 1 (8 items)