Wednesday, December 1, 2010

Handling Line Breaks with FQL in ASP.NET

Facebook's FQL query language lets you do some really cool things as far as pulling data from Facebook profiles, events, groups and more. I've been using it lately to create fun websites for various events I've been hosting, but I ran into a small issue while pulling the wall posts from a Facebook Group.

I was able to pull everything from the wall, but it all came as one long string with no html line breaks <br/>. Seems simple enough to fix, right? I ususally append a .Replace(vbCrLf, "<br/>") to my string and I'm golden, but that wasn't working this time.

I tried everything I could think of: vbCrLf, Environment.NewLine, \n, \r\n, \r, Chr(13), PHP_EOL, ControlChars.NewLine, ControlChars.CrLf - nothing seemed to be working until I finally tried out vbLf

The vbLf constant represents \n just as vbCr represents \r and vbCrLf represents \r\n. Facebook uses the \n notation to store line breaks.

So if I were to store my FQL (Facebook Query Language) query results in a string called myFqlResult, I could print it out to the screen in a decent viewing manner with the string:

lblMyExample.Text = myFqlResult.Replace(vbLf,"<br/>")

Facebook also throw in all sorts of xml structures as well, so I usually filter it all out with something like this:
lblMyExample.Text = myFqlResult.Replace("<message>", "<p>")_
.Replace("</message>", "</p>")_
.Replace("<stream_post>", "<div>")_
.Replace("</stream_post>", "</div>")_
.Replace(vbLf, "<br/>")_
.Replace("<fql_query_response xmlns=""http://api.facebook.com/1.0/"" _
xmlns:xsi=""http://www.w3.org/2001/XMLSchema-instance"" list=""true""><br/>", "")_
.Replace("</fql_query_response><br/>", "")_
.Replace("<?xml version=""1.0"" encoding=""UTF-8""?><br/>", "")_
.Replace("<div><br/>", "<div>")

0 comments:

Post a Comment