Parsing OPML Files with the RSS Component
OPML (Outline Processor Markup Language) is an XML format for outlines. It is commonly used to export and import lists of feed subscriptions (e.g., from feed readers and podcast applications), where each outline entry contains the URL of an RSS or Atom feed.
The RSS component can parse OPML files using the ReadOPML method. When it is called, the component parses the file, and fires the OPMLHeader event for each field in the header of the document (e.g., title or dateCreated), followed by the OPMLOutline event for each outline entry.
RSS rss = new RSS();
rss.OnOPMLHeader += (s, e) =>
{
Console.WriteLine(e.Field + ": " + e.Value);
};
rss.OnOPMLOutline += (s, e) =>
{
// Level is the depth of the entry in the outline, which can be used to show the hierarchy of the document.
string indent = new string(' ', e.Level * 2);
Console.WriteLine(indent + e.Text + (e.XMLURL.Length > 0 ? " (" + e.XMLURL + ")" : ""));
};
rss.ReadOPML("C:\\feeds\\subscriptions.opml");
The OPMLOutline event provides the common attributes of each outline entry as parameters:
| Parameter | Description |
|---|---|
| Level | The depth of the entry in the outline. |
| Text | The text of the entry (the text attribute). |
| TypeName | The type of the entry (the type attribute, e.g., rss). |
| Title | The title of the entry. |
| XMLURL | The URL of the feed (the xmlUrl attribute), for feed subscriptions. |
| HTMLURL | The URL of the web page for the feed (the htmlUrl attribute). |
| URL | The URL of the entry (the url attribute), for other types of entries. |
| Created | The date the entry was created. |
| Description | The description of the entry. |
| OtherAttrs | The names of any other attributes of the entry. |
The value of any other attribute can be retrieved by calling the OPMLAttr method with the name of the attribute inside the OPMLOutline event.
Reading OPML Files from a URL
The ReadOPML method reads a local file. To parse an OPML file from a web server, first download it to a local file using the HTTP component, and then pass the path of the file to ReadOPML.
HTTP http = new HTTP();
http.LocalFile = Path.Combine(Path.GetTempPath(), "subscriptions.opml");
http.Get("https://www.example.com/subscriptions.opml");
rss.ReadOPML(http.LocalFile);
Each feed found in the document can then be retrieved with the GetFeed method of the RSS component, using the URL in the XMLURL parameter.
We appreciate your feedback. If you have any questions, comments, or suggestions about this article please contact our support team at support@nsoftware.com.