Now that my blog is officially moved here it seems that the suitable first approach is how I moved it. If I hadn’t tried to figure out XPathNavigator and XPathIterator this would have taken an hour or so, but I spent the lat few hours trying these fancy XPathNavigator/XPathIterator classes and .NET’s System.Xml.Serialization.XmlSerializer to read RSS into my custom classes. Alas, I just went back to the trusty XML DOM via System.Xml.XmlDocument… Below is the code I used to move my entries. Note, I also made a slight modification to the generated "SBSSimpleBlogService" class so I could pass in the service URI .
class BlogMover
{
static void Main()
{
Uri rssSourceUri = new Uri("http://.../Hurricane/blogs/users/scott/rss.aspx");
Uri destServiceUri =
new Uri("http://blogs.pingpoet.com/overflow/services/SimpleBlogService.asmx");
BlogMover.MoveBlog(rssSourceUri, destServiceUri);
}
/// <summary>
/// Moves a blog from RSS 2.0 (hurricane) to the .Text SimpleWebService web service.
/// </summary>
/// <param name="sourceBlog"></param>
/// <param name="destServiceUri"></param>
public static void MoveBlog(System.Uri sourceBlog, System.Uri destServiceUri)
{
IList<BlogItem> blogItems = ReadBlog(sourceBlog);
PostItems(blogItems, destServiceUri);
}
/// <summary>
/// Reads an RSS2.0 feed and returns BlogItem's for it.
/// </summary>
/// <param name="blogUri"></param>
private static IList<BlogItem> ReadBlog(System.Uri blogUri)
{
WebClient client = new WebClient();
using (Stream inStream = client.OpenRead(blogUri.AbsoluteUri))
{
XmlReader reader = new XmlTextReader(inStream);
XmlDocument doc = new XmlDocument();
doc.Load(reader);
XmlNamespaceManager mgr = new XmlNamespaceManager(doc.NameTable);
//necessary for hurricane's <body xmlns="http://www.w3.org/....>
mgr.AddNamespace("w3", "http://www.w3.org/1999/xhtml");
XmlNodeList itemNodes = doc.SelectNodes("rss/channel/item");
List<BlogItem> blogItemList = new List<BlogItem>();
foreach (XmlNode itemNode in itemNodes)
{
BlogItem item = new BlogItem();
item.Title = itemNode.SelectSingleNode("title").InnerText;
item.Guid = itemNode.SelectSingleNode("guid").InnerText;
item.Link = itemNode.SelectSingleNode("link").InnerText;
string temp = itemNode.SelectSingleNode("pubDate").InnerText;
item.PubDate = BlogItem.ParseHurricaneDateTime(temp);
item.Body = itemNode.SelectSingleNode("w3:body", mgr).InnerText;
XmlNodeList categoryNodes = itemNode.SelectNodes("category");
List<string> categories = new List<string>();
foreach (XmlNode categoryNode in categoryNodes)
{
categories.Add(categoryNode.InnerText);
item.Categories = categories.ToArray();
}
blogItemList.Add(item);
}
return blogItemList;
}
}
/// <summary>
/// Posts the specified list of <see cref="BlogItem"/>s to the PingPoet.com "SimpleBlogService (http://".
/// </summary>
/// <param name="items"></param>
private static void PostItems(IList<BlogItem> items, Uri destServiceUri)
{
com.pingpoet.blogs.SBSSimpleBlogService blogService
= new com.pingpoet.blogs.SBSSimpleBlogService(destServiceUri.AbsoluteUri);
foreach (BlogItem item in items)
{
blogService.InsertCategoryPost("xxx", "xxx",
item.PubDate, item.Title, item.Body, item.Categories);
}
}
}
/// <summary>
/// Represents an "//rss/channel/item" item in RSS.
/// </summary>
[XmlType("item")]
public class BlogItem
{
public BlogItem()
{ }
[XmlElement("title")]
public string Title;
[XmlElement("guid")]
public string Guid;
[XmlElement("link")]
public string Link;
[XmlElement("pubDate")]
public DateTime PubDate;
[XmlElement("body")]
public string Body;
[XmlElement("Category")]
public string[] Categories;
public override string ToString()
{
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("title:").AppendLine(Title);
sb.Append("guid:").AppendLine(Guid);
sb.Append("link:").AppendLine(Link);
sb.Append("pubDate:").AppendLine(PubDate.ToString());
sb.Append("body:").AppendLine(Body);
if (Categories != null)
{
foreach (string cat in Categories)
{
sb.Append("\tCategory:").AppendLine(cat);
}
}
return sb.ToString();
}
public static DateTime ParseHurricaneDateTime(string dateStr)
{
DateTime dt = DateTime.Parse(dateStr);
return dt;
}
}
posted @ Tuesday, February 10, 2004 2:00 AM