RSS Blog Roller Code Update

I've updated the original blog roller code posted awhile back since a few minor changes were made.  On the homepage of Interface Technical Training we only show the first 250 characters of employee blogs.  However, people were using tools that injected so much HTML/CSS into their blog that no real text from the blog could be found in the first 250 characters (just HTML tags).  To remedy this problem, all HTML is now stripped out of the summary text and a small routine was added to cut-off summary text on a space rather than splitting a word in the middle. 

A simple Regular Expression is used to strip out HTML and a character array is used to find a good place to break-up the text for the summary.  The modified code is shown next:

public string FixDesc(object desc, object link)
{
    
if (link == null || desc == nullreturn String.Empty;

    string 
description desc.ToString();
    
//Replace all HTML tags so none get cut-off and screw up the page
    
Regex reg = new Regex("<.*?>",RegexOptions.Compiled);
    string 
stripDesc reg.Replace(description, String.Empty);
    if 
(stripDesc.Length > 250)
    {
        
int startPos 250;
        char
[] chars stripDesc.ToCharArray();
        char 
chars[startPos];
        while 
(!Char.IsWhiteSpace(c) && startPos < chars.Length)
        {
            startPos++
;
            
chars[startPos];
        
}
        stripDesc 
= new String(chars,0,startPos) + " ... <a href='" + link.ToString() + "'> More</a>";

    
}
    
return stripDesc;
}


The user control code that calls the FixDesc() method is shown here:

<asp:DataList ID="DataList1" runat="server" 
  DataSourceID
="odsRss" HorizontalAlign="Left" RepeatLayout="Flow">
   
<HeaderTemplate>
   
<p><a href="http://blogs.interfacett.com">
       (View All Instructor Blogs)
</a><br />
      </
HeaderTemplate>
    
<ItemTemplate>
        
<strong><a href='<%# Eval("Link") %>'><%# Eval("Title") %></a>
          
<br />
          
<%# Eval("PubDate") %> By <%# Eval("AuthorName") %></strong>
          
</p>
          
<p>
          
<%# FixDesc(Eval("Description"), Eval("Link"))%>
        
<br />     
    </
ItemTemplate>
    
<FooterTemplate>
    
</p>
    
</FooterTemplate>  
</asp:DataList>


The updated code can be found at the URL below:

http://www.xmlforasp.net/CodeBank/Download/Blog/BlogRollerWebsite.zip

 

comments powered by Disqus

No Comments