Syntax of If Statement Regarding XML in Silverlight WP7 App
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 simp开发者_如何学编程le 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
var arrow = (xmlitem.Element("title").Value.Contains("DATABASE Up"))
should actually be
var arrow = (item.Element("title").Value.Contains("DATABASE Up"))
You should be querying item, not xmlitem.
As mentioned in other answers, you should also be checking that elements exist before accessing their values.
if doesn't return a result that you can assign to var arrow.
Where you trying to do something like this?
string arrow = "";
if (xmlitem.Element("description").Value.Contains("DATABASE Up"))
{
arrow = ("up");
}
It's not entirely clear to me what you're trying to do. If the description
contains "DATABASE Up" you want the value of arrow
to be "up" otherwise what? null?
string arrow = null;
if (xmlitem.Element("description").Value.Contains("DATABASE Up"))
{
arrow = ("up");
}
or
var arrow = (xmlitem.Element("description").Value.Contains("DATABASE Up"))
? "up" : null;
EDIT
And why are you setting DATABASEinfoList.ItemsSource = list
both inside the foreach loop and again outside? The one inside should probably go away.
Also, there's an inherent problem, shown by your comment in Mick's answer.. These calls:
item.Element("[elementName]").Value
assume that the element exists. If it doesn't you'll call the Value
Property getter on a null, throwing a NullReferenceException. If there's a chance that the element would be null, then you need to check for that before calling Value:
var element = item.Element("[elementName]");
var value = (element == null) ? null : element.Value;
精彩评论