我正在解析XML。我通常使用下面示例代码的方式进行解析,这很简单。问题在于,我不拥有我要解析的XML,并且无法更改它。有时候没有缩略图元素(没有标签),我会收到一个异常。
有没有一种方法可以保持这种简单性并检查元素是否存在?或者我必须先使用LINQ获取
有没有一种方法可以保持这种简单性并检查元素是否存在?或者我必须先使用LINQ获取
XElement
列表,然后检查它并仅填充现有对象属性?void wc_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
XDocument dataDoc = XDocument.Load(new StringReader(e.Result));
var listitems = from noticia in dataDoc.Descendants("Noticia")
select new News()
{
id = noticia.Element("IdNoticia").Value,
published = noticia.Element("Data").Value,
title = noticia.Element("Titol").Value,
subtitle = noticia.Element("Subtitol").Value,
thumbnail = noticia.Element("Thumbnail").Value
};
itemList.ItemsSource = listitems;
}
parentEl.Element(elementName)
为空,则无法检索Value
属性。查看示例。仅在元素存在时才调用.Value
。 - Steve BmyObject.MyExtensionMethod()
而不是MyUtilityMethod(myobject)
。 - Steve B