0

我想知道用XML文件的内容执行if/then/else语句的最佳方式。更具体地说,我想根据某个字段的内容显示两个图像中的一个。例如,如果说明项目的内容是“红色”,我想显示一个红色按钮。如果它是“绿色”,那么是绿色的图像。这是在Visual Studio 2010在这里做一个Silverlight WP7应用程序是我的代码的情况下:如果然后关于XML文件内容的其他语句

public MainPage() 
    { 
     InitializeComponent(); 

     Dispatcher.BeginInvoke((Action)(() => DATABASEinfoList.ItemsSource = list)); 

     WebClient DB = new WebClient(); 

     DB.DownloadStringCompleted += new DownloadStringCompletedEventHandler(DATABASEinfo_DownloadStringCompleted); 
     DB.DownloadStringAsync(new Uri("http://www.URL.com/index.xml")); 
    } 


    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").Value; 
      var description = item.Element("description").Value; 


      list.Add(new DATABASEinfoViewModel 
      { 
       Title = title, 
       Description = description, 
      }); 
     } 


     DATABASEinfoList.ItemsSource = list; 


    } 



    public class DATABASEinfoViewModel 
    { 
     public string Title { get; set; } 
     public string Description { get; set; } 
    } 

回答

2
if (xmlitem.Element("color").Value.Equals("Red")) { 
    // ... 
}