by Pieter Brinkman
3. March 2009 09:10
For dotNetSearch.net I needed to cache the RSS feed (on the right). I did this with the .Net Cache namespace.
if(Cache.Get(RssUrl) == null)
{
RssDocument rssFeed = RssDocument.Load(new Uri(RssUrl));
Cache.Insert(RssUrl, XDocument.Parse(rssFeed.ToXml(DocumentType.Rss)), null, DateTime.Now.AddMinutes(15), TimeSpan.Zero);
}
XDocument rssXml = (XDocument)Cache.Get(RssUrl);
In this examle I add the RSSFeed content as object in the cache and use the url of the RSS feed as Key. The Cache will expire (and cleared) after 15 minutes.
by Pieter Brinkman
8. December 2008 05:31
I was using some web-services that returned there values in an XmlElement. I needed to convert this XmlElement to something that I can use with LINQ. I didn't find good solutions on the internet. So I started building my own convert logic. After all kinds of solutions I ended up with creating an new XmlDocument, adding the XmlElement to the XmlDocument and then convert the innerXml of the XmlDocument to an XElement. Not really nice but it does the trick.
I've rewritten the code into a Extension Method for the XmlElement.
public static XElement ToXElement(this XmlElement xml)
{
XmlDocument doc = new XmlDocument();
doc.AppendChild(doc.ImportNode(xml, true));
return XElement.Parse(doc.InnerXml);
}
You use the Extension Method like this:
XmlElement xmlElement = wsClient.DoWebServiceRequest();
XElement xml = xmlElement.ToXElement();
Please tell me if you have a better way of doing this!
Cheers,
Pieter