c# - OPML Parser

Well, I needed a opml parser for the widget i'm currently building using the mooTools. Since I couldn't found one that was standalone, i decided to do one.

The code is simple to use. It can be done in to ways: Parse a existing file, or create a new structure and the save it.

Let's parse one :

//Parse a existing file
Opml data = Opml.Parse(Server.MapPath("app_Data/Data.opml"));

Now that I've got the opml structure, let's add a new outline:

// add new outline
OpmlOutline myOutline = new OpmlOutline();
myOutline.Text = "CreativeMinds Blog";
myOutline.Title = "CreativeMinds Blog";
myOutline.Description = "CreativeMinds - because we all have one...";
myOutline.HtmlUrl = new Uri("http://www.brunofigueiredo.com");
myOutline.XmlUrl = new Uri("http://www.brunofigueiredo.com/rss.aspx");
data.AddOutline(myOutline);

For navigate the outline, you can use the new functionality of the generics lists, the foreach:

// navigate the outlines
data.Outlines.ForEach(delegate(OpmlOutline outline)
{
Response.Write(outline.Text + "<br>");
});


In the end,  you can save to a file:
// save to a new file
data.Save(Server.MapPath("app_Data/clone.opml"));


The download can be done where.

Related posts

Comments

May 26. 2007 05:33 AM

Unresolvable

Line 176 reads:

dateCreated.InnerText = DateModified != DateTime.MinValue ? DateModified.ToString("r", null) : DateTime.Now.ToString("r", null);

When it should be:

dateModified.InnerText = DateModified != DateTime.MinValue ? DateModified.ToString("r", null) : DateTime.Now.ToString("r", null);

Unresolvable

May 26. 2007 05:49 AM

Unresolvable

Oh and line 204 says:

outlineNode.SetAttribute("text", outline.Title);

and should be:

outlineNode.SetAttribute("title", outline.Title);


and line 208 reads:

outlineNode.SetAttribute("title", outline.Text);

and should be:

outlineNode.SetAttribute("text", outline.Text);

Unresolvable

March 28. 2008 08:25 AM

Salvatore Ansani

I found a BUG processing *outline* XML tags. You didn't consider outline childs...
For example, if you have something like this:

<outline title="outline DIR" text="outline DIR">
<outline ...rss item.../>
<outline ...another rss item.../>
</outline>

Content between <outline></outline> tags is ignored (!!!!).

You need to modify opml.cs with something like this:

foreach (XmlElement outline in outlineList)
{
if (outline.HasChildNodes)
{
XmlNodeList outlineChildList = outline.SelectNodes("./outline");
foreach (XmlElement outlineChild in outlineChildList)
{
_out.Outlines.Add(ParseContent(outlineChild));
}
}
else
{
_out.Outlines.Add(ParseContent(outline));
}

}

Regards,
Salvatore

Salvatore Ansani

April 30. 2008 05:26 PM

Bruno 'Shine' Figueiredo

Thanks Salvatore.
I'm merging all my code bits in a unique assembly. As soon I have all things done I'll update the link.

Once again thanks.

Bruno 'Shine' Figueiredo

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Preview

July 25. 2008 09:10 PM

About Me

I'm a Senior Consultant in the work, and a geek outside of it :P

Recent posts

Recent comments