everyone! I couldn’t find a tutorial explaining the right way to code this. I think it will be clear from the title and the code what I’m trying to do. The two errors I’m receiving is that my if statement is in the wrong place, and that the variable ‘Arrow’ is assigned but never used. I know this comes down to simple syntax, so I thank everyone for their time.
void DATABASEinfo_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Error != null)
return;
XElement xmlitem = XElement.Parse(e.Result);
var list = new List<DATABASEinfoViewModel>();
foreach (XElement item in xmlitem.Element("channel").Elements("item"))
{
var title = item.Element("title");
var titlevalue = (title == null) ? null : title.Value;
var description = item.Element("description");
var descriptionvalue = (description == null) ? null : description.Value;
var arrow = (xmlitem.Element("title").Value.Contains("DATABASE Up"))
? "up" : null;
list.Add(new DATABASEinfoViewModel
{
Title = titlevalue,
Description = descriptionvalue,
Arrow = arrow,
});
}
DATABASEinfoList.ItemsSource = list;
}
public class DATABASEinfoViewModel
{
public string Title { get; set; }
public string Description { get; set; }
public string Arrow { get; set; }
Oddly, if I change:
var arrow = (xmlitem.Element("title").Value.Contains("DATABASE Up"))
To:
var arrow = (xmlitem.Element("channel").Value.Contains("DATABASE Up"))
It displays “up” for ALL entries. Here is an example of the XML file:
<rss version="2.0">
<channel>
<title> DATABASE Status</title>
<description>DATABASE status updates</description>
<item>
<title>First status is DATABASE Up</title>
<description>First Content</description>
</item>
<item>
<title>Second status is DATABASE Up</title>
<description>Second Content</description>
</item>
</channel>
The line
should actually be
You should be querying item, not xmlitem.
As mentioned in other answers, you should also be checking that elements exist before accessing their values.