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 DateFile dynamically change based on URI paramaters 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, so I can alter the <asp.Hyperlink> URL so it contains that value?
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?
For those who may be interested in this as well, I found a solution where I can easily check and manipulate xml items.
Code Behind C#
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
}
}
Marked as answer by HVMDesign on Nov 30, 2012 08:43 PM
HVMDesign
0 Points
2 Posts
XPath / Grabbing node values in Code Behind
Nov 29, 2012 08:30 PM|LINK
Hello,
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 DateFile dynamically change based on URI paramaters 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, so I can alter the <asp.Hyperlink> URL so it contains that value?
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?
THANK YOU!
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=" + Request.QueryString["cat"]; } // insert search query if(searchTerms != null){ searchQueries += "&s=" + searchTerms; } // load RSS file rssFeed.DataFile = rssSource + searchQueries; rssFeed.XPath = rssPath; } // return message if results are empty 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
}
HVMDesign
0 Points
2 Posts
Re: XPath / Grabbing node values in Code Behind
Nov 30, 2012 08:43 PM|LINK
For those who may be interested in this as well, I found a solution where I can easily check and manipulate xml items.
Code Behind C#
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 } }