不存在摘要节点时的LINQ到XML异常

不存在摘要节点时的LINQ到XML异常,linq,linq-to-xml,Linq,Linq To Xml,我一直在使用LINQtoXML,遇到了一个问题。我真的很感激任何帮助。我不熟悉LINQtoXML,但我发现它很容易使用 我有两个不同的联合提要,我使用Union将它们聚合为一个联合提要。最终的联合提要包含10个项目 我正在尝试使用XDocument和XElement将联合提要写入XML文件。我大部分时间都能成功地做到这一点。但是,提要中的某些项没有作为节点元素的描述。当我到达没有这个节点元素的项目时,我得到了一个异常,因为我没有其中一个项目的描述节点。在开始编写XML文件之前,如何检查这些项以查

我一直在使用LINQtoXML,遇到了一个问题。我真的很感激任何帮助。我不熟悉LINQtoXML,但我发现它很容易使用

我有两个不同的联合提要,我使用Union将它们聚合为一个联合提要。最终的联合提要包含10个项目

我正在尝试使用XDocument和XElement将联合提要写入XML文件。我大部分时间都能成功地做到这一点。但是,提要中的某些项没有作为节点元素的描述。当我到达没有这个节点元素的项目时,我得到了一个异常,因为我没有其中一个项目的描述节点。在开始编写XML文件之前,如何检查这些项以查看是否存在名为description的节点?如果项目不包含描述节点,如何使用默认值填充它?你能给我建议一些解决办法吗?谢谢你花了这么多时间

SyndicationFeed combinedfeed = new SyndicationFeed(newFeed1.Items.Union(newFeed2.Items).OrderByDescending(u => u.PublishDate));

//save the filtered xml file to a folder
XDocument filteredxmlfile = new XDocument(
             new XDeclaration("2.0", "utf-8", "yes"),
             new XElement("channel",
             from filteredlist in combinedfeed.Items
             select new XElement("item",
                  new XElement("title", filteredlist.Title.Text),
                  new XElement("source", FormatContent(filteredlist.Links[0].Uri.ToString())[0]),
                  new XElement("url", FormatContent(filteredlist.Links[0].Uri.ToString())[1]),
                  new XElement("pubdate", filteredlist.PublishDate.ToString("r")),
                  new XElement("date",filteredlist.PublishDate.Date.ToShortDateString()),
// I get an exception here as the summary/ description node is not present for all the items in the syndication feed
new XElement("date",filteredlist.Summary.Text)
                  )));
string savexmlpath = Server.MapPath(ConfigurationManager.AppSettings["FilteredFolder"]) + "sorted.xml";
filteredxmlfile.Save(savexmlpath);

只需检查
null

new XElement("date",filteredlist.Summary !=null ? filteredlist.Summary.Text : "default summary")

你好,博肯格拉斯,谢谢你的回复。我使用了您在上面提供的代码,但它给了我一个对象空引用异常。因此,我将其更改为:new-XElement(“description”,filteredlist.Summary.Text!=null?filteredlist.Summary.Text:“default Summary”)。执行此操作时,它甚至没有在xml文件中创建此节点元素。没有例外什么都没有。我不确定,但我想在开始编写xml文件之前,我需要检查每个联合提要项中是否存在描述节点。谢谢你抽出时间!