Question

I am fairly new to ASP.NET (PHP programmer for 8 years), so please forgive me if my terminology is incorrect.

I am currently grabbing a remote XML file in a repeater XMLDataSource, and the DataFile attribute is being set in the code behind (I have the DataFile dynamically change based on URI parameters such as ?cat=blah etc). This works beautifully and is not a problem.

Within this repeater is a <asp:HyperLink>, whose URL is to be dynamically changed in the code behind as well.

Now to the question:

From the code behind, how can I see if XML nodes are empty, and how can I grab the value of nodes that are not?

I have been on this issue for about 3 days now (hours of searching), however I just cannot figure out something that seems like it should be so simple! Here is the code I currently have, however I am given an error of Object reference not set to an instance of an object for the code behind line that has CreateNavigator();

Hopefully somebody more adept than me with ASP.NET and C# can easily spot the problem or knows of a way I can accomplish this?

Code

<asp:XmlDataSource ID="rssFeed" runat="server"></asp:XmlDataSource>
<asp:Repeater runat="server" DataSourceID="rssFeed" OnItemDataBound="Repeater_ItemDataBound">
        <ItemTemplate>
                <asp:HyperLink ID="articleURL" Text='<%#XPath("title")%>' runat="server" />
        </ItemTemplate>
</asp:Repeater>

Code behind C#

protected void Page_Load(object sender, EventArgs e)
{
        // rss variables
        var rssSource = "http://websiteurl.com/xmlfeed/";
        var rssPath = "rss/channel/item";
        var searchQueries = "?foo=bar";
        string currCat = Request.QueryString["cat"];
        string searchTerms = Request.QueryString["s"];

        // insert categories query
        if(currCat != null){
                searchQueries += "&cat=" + currCat;
        }

        // insert search query
        if(searchTerms != null){
                searchQueries += "&s=" + searchTerms;
        }

        // load RSS file
        rssFeed.DataFile = rssSource + searchQueries;
        rssFeed.XPath = rssPath;
}

protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
        XPathNavigator nav = ((IXPathNavigable)e.Item.DataItem).CreateNavigator();
        string hyperlinkURL = nav.SelectSingleNode("link").Value;
        // removed code here that changes URL to hyperlinkURL to shorten post
 }
Was it helpful?

Solution

For those who may be interested in this as well, I found a solution where I can easily check and manipulate xml items.

protected void Repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    // make sure we are not checking item in ItemHeader or ItemFooter to prevent null value
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem) {
        string myVar = XPathBinder.Eval(e.Item.DataItem,"title").ToString();

        // utilize and manipulate myVar XML value here
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top