the filename would be httpwwwwroxcomWileyCDAfeedRSSWROXALLNEWxml.xml. To strip off all the special characters in the URL, define the RemoveSpecialChars()
function as follows:
//---removes special chars from an URL string---
private string RemoveSpecialChars(string str) {
string NewString = String.Empty;
Regex reg = new Regex('[A-Z]|[a-z]');
MatchCollection coll = reg.Matches(str);
for (int i = 0; i <= coll.Count - 1; i++)
NewString = NewString + coll[i].Value;
return NewString;
}
You use the Regex
(regular expression) class to extract all the alphabets from the URL and append them into a string, which will be returned to the calling function to use as a filename.
Next, define the SubscribeFeed()
function to subscribe to a feed, and then add each post to the TreeView
control (see Figure 18-13):
//---returns true if subscription is successful---
private bool SubscribeFeed(string URL) {
bool succeed = false;
try {
//---display the wait message panel---
if (displayPanel == null) {
displayPanel = CreatePanel('Downloading feed...Please wait.');
this.Controls.Add(displayPanel);
} else {
displayPanel.BringToFront();
displayPanel.Visible = true;
Cursor.Current = Cursors.WaitCursor;
//---update the UI---
Application.DoEvents();
}
//---download feed---
string title = String.Empty;
string[] posts = DownloadFeed(URL, ref title).Split((char)12);
if (posts.Length > 0 && posts[0] != String.Empty) {
//---always add to the root node---
TreeNode FeedTitleNode = new TreeNode() {
Text = title,
Tag = URL, //---stores the Feed URL---
ImageIndex = ICO_CLOSE,
SelectedImageIndex = ICO_OPEN
};
//---add the feed title---
TreeView1.Nodes[0].Nodes.Add(FeedTitleNode);
//---add individual elements (posts)---
for (int i = 0; i <= posts.Length - 2; i++) {
//---extract each post as 'title:description'---
string[] str = posts[i].Split((char)3);
TreeNode PostNode = new TreeNode() {
Text = str[0], //---title---
Tag = str[1], //---description---
ImageIndex = ICO_POST,
SelectedImageIndex = ICO_POST
};
//---add the posts to the tree---
TreeView1.Nodes[0].Nodes[TreeView1.Nodes[0].Nodes.Count - 1].Nodes.Add (PostNode);
}
//---subscription is successful---
succeed = true;
//---highlight the new feed and expand its post---
TreeView1.SelectedNode = FeedTitleNode;
} else succeed = false;
} catch (Exception ex) {
MessageBox.Show(ex.Message);
//---subscription is not successful---
succeed = false;
} finally {
//---clears the panel and cursor---
Cursor.Current = Cursors.Default;
displayPanel.Visible = false;
//---update the UI---
Application.DoEvents();
}
return succeed;
}

Figure 18-13
For each TreeView node representing a feed title (such as Wrox: All New Titles), the Text
property is set to the feed's title and its URL is stored in the Tag
property of the node. For each node representing a posting (.NET Domain-Driven Design and so forth), the Text
property is set to the posting's title and its description is stored in the Tag property.
Wiring All the Event Handlers
With the helper functions defined, let's wire up all the event handlers for the various controls. First, code the Form1_Load
event handler as follows:
private void Form1_Load(object sender, EventArgs e) {
//---find out the app's path---
appPath = Path.GetDirectoryName(
System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
//---set the feed list to be stored in the app's folder---
feedsList = appPath + feedsList;
try {
//---create the root node---
TreeNode node = new TreeNode() {
ImageIndex = ICO_CLOSE,
SelectedImageIndex = ICO_OPEN,