LINQ to XML exception when there is no summary node present

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hello All,<br/>
<br/>
I have been working with LINQ to XML and have been stuck with an issue. I would really appreciate any help. I am new to LINQ to XML, but I found it easy to work with.<br/>

I have two different syndication feeds that I aggregate to one single syndication feed using Union. The final syndication feed contains 10 items.I am trying to write the syndication feed to an XML file using XDocument and XElement. I have been able to do
that successfully for the most part. But, some of the items in the feed do not have a description as a node element. When I get to the items that do not have this node element I am getting an Exception as I don’t have a description node for one of the items.
How can I check the items to see if there is a node called description before I start writing the XML file? If the item does not contain the description node how could I populate it with a default value? Could you please suggest me any solution? Thank you
for all your time!
<pre class="prettyprint SyndicationFeed combinedfeed = new SyndicationFeed(newFeed1.Items.Union(newFeed2.Items).OrderByDescending(u => u.PublishDate));


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); [/code]
<br/>

View the full article
 
Back
Top