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.
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!
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);
you do not specify the type of exception that you're getting.
i'm guessing that you'd like filteredlist.Summary.Text
to be a string; i'm also guessing that you're getting an exception because
filteredlist.Summary.Text is null.
You could trying replacing filteredlist.Summary.Text with a method that returns a string, where the string is either
filteredlist.Summary.Text or an empty string when filteredlist.Summary.Text
is null:
public string sss(string abc)
{
if (abc != null) return abc; else return String.Empty;
}
thus: new XElement("date",filteredlist.Summary.Text)
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
Thank you for your response. Sorry, i did not specify the exception in my previous post: I am getting a System.NullReferenceException: Object reference not set to an instance of an object.
I have tried the code you have provided me, but I am still getting the exception. The syndication feed generates 10 final items. Some of these items don't have a description node. So, I think I need to find a way to check if all the items in the syndication
feed have the description node and then use select to select the nodes values from each of the items. Presently with my code I am directly doing a Select to find the values of the nodes without checking if the node exists or not. I am new to LINQ, so I am
trying a way to find if the node exists before even doing the select. If you have any other suggestions please let me know. Thank you all for your time.
SyndicationFeed combinedfeed = new SyndicationFeed(newFeed1.Items.Union(newFeed2.Items).OrderByDescending(u => u.PublishDate));
//first check it the description node exists in the syndication feed item if so do this below------------
//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)
)));
search videos at Google with debug visual studio 2010
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
also, it's a good idea to show your peers here at forums.asp.net your stacktrace from the exception ... stacktraces often reveal clues.
another place that is useful is the Windows Event Log.
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
Not sure but try like this: new xElement("date", (filteredlist.Summary == null) ? "" : filteredlist.Summary.Text). Instead of the empty string you can use whatever value that's reasonable for your scenario (assuming my idea actually works).
If the item does not contain the description node how could I populate it with a default value?
Just use my above codes,If not exists,plz ignore it or just directly output a default value by using Response.Write or Console.Write in the Console App。
if your XML is bad, XML Notepad 2007 will usually point out the defective line.
if your XML is good, XML Notepad 2007 is a decent, free tool for browsing your XML manually ... it's a tool you need in your developer's toolkit, imho.
g.
B-) Please help me by completing my school survey about computer programmers on my website. Thank you!!! Gerry Lowry +1 705-429-7550 wasaga beach, ontario, canada
Thank you all for your responses. I am able to fix this issue by not using LINQ to XML to write the XML file rather using XmlTextWriter class. It was more code that LINQ to XML, but it gave me a way to check for the description and if it is null, populate
it with a default value. I can also use try catch if need be while using XmlTextWrite class. Where as previously, I was not able to do any checking like "if" or "try catch" inside the Select LINQ query.
Thanks you to gerrylowry , deckerdong, metalasp.net for all the pointers.
smksu
Member
69 Points
34 Posts
LINQ to XML exception when there is no summary node present
Feb 11, 2012 02:54 AM|LINK
Hello All,
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.
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!
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);gerrylowry
All-Star
20513 Points
5712 Posts
Re: LINQ to XML exception when there is no summary node present
Feb 12, 2012 01:32 AM|LINK
@ smksu
i suspect this will work, you'll need to try it:
you do not specify the type of exception that you're getting.
i'm guessing that you'd like filteredlist.Summary.Text to be a string; i'm also guessing that you're getting an exception because filteredlist.Summary.Text is null.
You could trying replacing filteredlist.Summary.Text with a method that returns a string, where the string is either filteredlist.Summary.Text or an empty string when filteredlist.Summary.Text is null:
public string sss(string abc) { if (abc != null) return abc; else return String.Empty; }thus: new XElement("date",filteredlist.Summary.Text)
becomes: new XElement("date",sss(filteredlist.Summary.Text))
g.
smksu
Member
69 Points
34 Posts
Re: LINQ to XML exception when there is no summary node present
Feb 12, 2012 02:44 PM|LINK
Hello grerry,
Thank you for your response. Sorry, i did not specify the exception in my previous post: I am getting a System.NullReferenceException: Object reference not set to an instance of an object.
I have tried the code you have provided me, but I am still getting the exception. The syndication feed generates 10 final items. Some of these items don't have a description node. So, I think I need to find a way to check if all the items in the syndication feed have the description node and then use select to select the nodes values from each of the items. Presently with my code I am directly doing a Select to find the values of the nodes without checking if the node exists or not. I am new to LINQ, so I am trying a way to find if the node exists before even doing the select. If you have any other suggestions please let me know. Thank you all for your time.
SyndicationFeed combinedfeed = new SyndicationFeed(newFeed1.Items.Union(newFeed2.Items).OrderByDescending(u => u.PublishDate)); //first check it the description node exists in the syndication feed item if so do this below------------ //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) )));gerrylowry
All-Star
20513 Points
5712 Posts
Re: LINQ to XML exception when there is no summary node present
Feb 12, 2012 03:01 PM|LINK
@ smksu
Have you walked your code with your debugger*?
video, c. 8 minutes: http://msdn.microsoft.com/en-ca/vstudio/ee672313.aspx
"How Do I: Step with The Debugger in Visual Studio?"
g.
* Debugging: http://lmgtfy.com/?q=debug+visual+studio+2010
example: http://www.codeproject.com/KB/cs/MasteringInDebugging.aspx
videos: http://www.youtube.com/watch?v=z5gBIizwsY0
search videos at Google with debug visual studio 2010
gerrylowry
All-Star
20513 Points
5712 Posts
Re: LINQ to XML exception when there is no summary node present
Feb 12, 2012 03:03 PM|LINK
@ smksu
also, it's a good idea to show your peers here at forums.asp.net your stacktrace from the exception ... stacktraces often reveal clues.
another place that is useful is the Windows Event Log.
g.
MetalAsp.Net
All-Star
112085 Points
18242 Posts
Moderator
Re: LINQ to XML exception when there is no summary node present
Feb 12, 2012 03:35 PM|LINK
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: LINQ to XML exception when there is no summary node present
Feb 13, 2012 12:24 AM|LINK
Suppose you've got an xml file or an xml file,just do this plz by use Descedants method——
var result = XDocument.Load("xxx.xml").Descedants("description");
if(result!=null && result.Count()>0)
{
……………………
}
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: LINQ to XML exception when there is no summary node present
Feb 13, 2012 12:26 AM|LINK
Just use my above codes,If not exists,plz ignore it or just directly output a default value by using Response.Write or Console.Write in the Console App。
Reguards!
gerrylowry
All-Star
20513 Points
5712 Posts
Re: LINQ to XML exception when there is no summary node present
Feb 13, 2012 02:51 AM|LINK
@ smksu
tip: if you do not already have it, get the free XML NotePad 2007 ...
http://www.microsoft.com/download/en/details.aspx?displaylang=en&id=7973
"XML Notepad 2007 provides a simple intuitive user interface for browsing and editing XML documents."
if your XML is bad, XML Notepad 2007 will usually point out the defective line.
if your XML is good, XML Notepad 2007 is a decent, free tool for browsing your XML manually ... it's a tool you need in your developer's toolkit, imho.
g.
smksu
Member
69 Points
34 Posts
Re: LINQ to XML exception when there is no summary node present
Feb 15, 2012 02:35 AM|LINK
Hello All,
Thank you all for your responses. I am able to fix this issue by not using LINQ to XML to write the XML file rather using XmlTextWriter class. It was more code that LINQ to XML, but it gave me a way to check for the description and if it is null, populate it with a default value. I can also use try catch if need be while using XmlTextWrite class. Where as previously, I was not able to do any checking like "if" or "try catch" inside the Select LINQ query.
Thanks you to gerrylowry , deckerdong, metalasp.net for all the pointers.