Created this back in 2008 or 09. Originally was an experiment in trying to pull RSS/XML feeds cross server in a Microsoft environment utilizing a repeater control. It turned out to be way more useful than ever expected. One of the reasons, it's so simple it's just robust. That and wrapping it in a try/catch didn't hurt =).
/* | |
* 1. furl = Url/Path, the url/path of your feed | |
* 2. xpathexp = XPath, Xpath Expression that defaults to pull the first set of nodes, use this to control entries, which ones and how many | |
* 3. sortby = Sort by, Another XPath Expression tell pointing OrderBy at a particular attribute or node value | |
* 4. orderby = Order by, Can be "Ascending" or "Descending", defaults to "Ascending" | |
* 5. sortas = Sort as, Can be "Text" or "Number", defaults to "Text" | |
* 6. namespcname = Namespace Name, If you have one... example "atom" | |
* 7. namespcurl = Namespace Url, If you have one... example "http://www.w3.org/2005/Atom" | |
*/ | |
public void XMLFeed(Repeater cobj, string furl, string xpathexp, string sortby, string orderby, string sortas, string namespcname, string namespcurl) | |
{ | |
try | |
{ | |
if (xpathexp == "") | |
{ | |
xpathexp = "//*[count(ancestor::*) = 1]"; | |
} | |
System.Xml.XPath.XPathDocument xdoc = new System.Xml.XPath.XPathDocument(furl); | |
System.Xml.XPath.XPathNavigator xnav = xdoc.CreateNavigator(); | |
System.Xml.XPath.XPathExpression xexp = xnav.Compile(xpathexp); | |
System.Xml.XmlNamespaceManager xmgn = new System.Xml.XmlNamespaceManager(xnav.NameTable); | |
if (namespcname != "" && namespcurl != "") | |
{ | |
xmgn.AddNamespace(namespcname, namespcurl); | |
} | |
xexp.SetContext(xmgn); | |
if (sortby != "") | |
{ | |
System.Xml.XPath.XPathExpression xsort = xnav.Compile(sortby); | |
xsort.SetContext(xmgn); | |
System.Xml.XPath.XmlSortOrder xorder = System.Xml.XPath.XmlSortOrder.Ascending; | |
if (System.String.Compare(orderby, "Ascending", true) == 0) | |
{ | |
xorder = System.Xml.XPath.XmlSortOrder.Ascending; | |
} | |
else if (System.String.Compare(orderby, "Descending", true) == 0) | |
{ | |
xorder = System.Xml.XPath.XmlSortOrder.Descending; | |
} | |
System.Xml.XPath.XmlDataType xsortas = System.Xml.XPath.XmlDataType.Text; | |
if (System.String.Compare(sortas, "Text", true) == 0) | |
{ | |
xsortas = System.Xml.XPath.XmlDataType.Text; | |
} | |
else if (System.String.Compare(sortas, "Number", true) == 0) | |
{ | |
xsortas = System.Xml.XPath.XmlDataType.Number; | |
} | |
xexp.AddSort(xsort, xorder, System.Xml.XPath.XmlCaseOrder.None, String.Empty, xsortas); | |
} | |
System.Xml.XPath.XPathNodeIterator xni = xnav.Select(xexp); | |
cobj.DataSource = xni; | |
cobj.DataBind(); | |
cobj.EnableViewState = false; | |
} | |
catch (Exception e) | |
{ | |
cobj.Controls.Add(new LiteralControl("<!--" + e + "-->")); | |
} | |
} |
/* Generic XMLFeed */ | |
/* | |
Had to update the bit of oldskool recently, added a date comparison. This thing won't quit, | |
oh and framework 2 limitations, ughhh. I suppose I could have placed some overload methods in there... that's your job =) | |
*/ | |
/* | |
* 1. furl = Url/Path, the url/path of your feed | |
* 2. xpathexp = XPath, Xpath Expression that defaults to pull the first set of nodes, use this to control entries, which ones and how many | |
* 3. sortby = Sort by, Another XPath Expression tell pointing OrderBy at a particular attribute or node value | |
* 4. orderby = Order by, Can be "Ascending" or "Descending", defaults to "Ascending" | |
* 5. sortas = Sort as, Can be "Text" or "Number", defaults to "Text" | |
* 6. namespcname = Namespace Name, If you have one... example "atom" | |
* 7. namespcurl = Namespace Url, If you have one... example "http://www.w3.org/2005/Atom" | |
*/ | |
namespace AWESOME | |
{ | |
using System.Xml; | |
using System.Xml.XPath; | |
public class AWESOMECLASS | |
{ | |
public void RepeaterFeed(Repeater ControlObj, | |
string furl, | |
string xpathexp, | |
string sortby, | |
string orderby, | |
string sortas, | |
string namespcname, | |
string namespcurl) | |
{ | |
try | |
{ | |
if (xpathexp == "") | |
{ | |
xpathexp = "//*[count(ancestor::*) = 1]"; | |
} | |
XPathDocument xdoc = new XPathDocument(furl); | |
XPathNavigator xnav = xdoc.CreateNavigator(); | |
XPathExpression xexp = xnav.Compile(xpathexp); | |
XmlNamespaceManager xmgn = new XmlNamespaceManager(xnav.NameTable); | |
if (namespcname != "" && namespcurl != "") | |
{ | |
xmgn.AddNamespace(namespcname, namespcurl); | |
} | |
xexp.SetContext(xmgn); | |
if (sortby != "") | |
{ | |
XPathExpression xsort = xnav.Compile(sortby); | |
xsort.SetContext(xmgn); | |
if(System.String.Compare(sortas, "Date", true) == 0) | |
{ | |
if (System.String.Compare(orderby, "Descending", true) == 0) | |
{ | |
xexp.AddSort(xsort, new DateTimeComparerAscending()); | |
} | |
else | |
{ | |
xexp.AddSort(xsort, new DateTimeComparerDescending()); | |
} | |
} | |
else | |
{ | |
XmlSortOrder xorder = XmlSortOrder.Ascending; | |
if (System.String.Compare(orderby, "Descending", true) == 0) | |
{ | |
xorder = XmlSortOrder.Descending; | |
} | |
XmlDataType xsortas = XmlDataType.Text; | |
if (System.String.Compare(sortas, "Number", true) == 0) | |
{ | |
xsortas = XmlDataType.Number; | |
} | |
xexp.AddSort(xsort, xorder, XmlCaseOrder.None, String.Empty, xsortas); | |
} | |
} | |
XPathNodeIterator xni = xnav.Select(xexp); | |
ControlObj.DataSource = xni; | |
ControlObj.DataBind(); | |
ControlObj.EnableViewState = false; | |
} | |
catch (Exception e) | |
{ | |
ControlObj.Controls.Add(new LiteralControl("<!--" + e + "-->")); | |
} | |
} | |
protected class DateTimeComparerAscending : IComparer | |
{ | |
public int Compare(object x, object y) | |
{ | |
DateTime dt1 = DateTime.Parse(x.ToString()); | |
DateTime dt2 = DateTime.Parse(y.ToString()); | |
return dt2.CompareTo(dt1); | |
} | |
} | |
protected class DateTimeComparerDescending : IComparer | |
{ | |
public int Compare(object x, object y) | |
{ | |
DateTime dt1 = DateTime.Parse(x.ToString()); | |
DateTime dt2 = DateTime.Parse(y.ToString()); | |
return dt1.CompareTo(dt2); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment