Merging XML using Visual Basic 9

Published 13 November 07 10:06 AM

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!

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# Rick Hodder said on November 14, 2007 5:33 PM:

Great sample beth!

# Phil Richard said on November 16, 2007 10:57 AM:

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

# Beth Massi said on November 16, 2007 12:16 PM:

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

# The Visual Basic Team said on November 21, 2007 1:35 AM:

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

# Noticias externas said on November 21, 2007 1:56 AM:

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

# Philipp Merz said on November 21, 2007 4:37 AM:

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.

# Beth Massi - Sharing the goodness that is VB said on November 21, 2007 2:45 PM:

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

# Noticias externas said on November 21, 2007 3:24 PM:

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

Leave a Comment

(required) 
(optional)
(required) 

  
Enter Code Here: Required

About Beth Massi

Beth is a Program Manager on the Visual Studio Community Team at Microsoft and is responsible for producing and managing content for business application developers, driving community features and team participation onto MSDN Developer Centers (http://msdn.com), and helping make Visual Studio one of the best developer tools in the world. She also produces regular content on her blog (http://blogs.msdn.com/bethmassi), Channel 9, and a variety of other developer sites and magazines. As a community champion and a long-time member of the Microsoft developer community she also helps with the San Francisco East Bay .NET user group and is a frequent speaker at various software development events. Before Microsoft, she was a Senior Architect at a health care software product company and a Microsoft Solutions Architect MVP. Over the last decade she has worked on distributed applications and frameworks, web and Windows-based applications using Microsoft development tools in a variety of businesses. She loves teaching, hiking, mountain biking, and driving really fast.

This Blog

Syndication

Page view tracker