RSS.NET class library - check invalid feed - c#

I'm currently using the RSS.MET class library to open and read feeds, but I need to be able to find out if a supplied feed is actually valid. For example, if I pass it "http://www.google.com", I want it to tell me that it isn't a valid RSS feed. How would I go about doing this?
I've already tried passing it through a try .. catch block.
try
{
Rss.RssReader reader = new Rss.RssReader(cast.PodcastURL);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
But that hasn't returned the desired effect.

According to the documentation, RssReader.Read will throw an exception if it can't read the rss file. (I guess it will be an XmlException).

Well if for some reason you don't want to work with the exception method, you could load the data in a xml file and check whether it has a top node rss...
I have worked extensively with RSS and Atom feeds but haven't used any special class to handle... Linq to XML makes handling raw XML pretty easy... :)

Related

Huge amount of data retrieved from database causes System.AccessViolationException

We have a code which is extracting data from Oracle database. The data returned from the database is in XML format and it gets returned as ref cursor as it may contains multiple XML's. Each xml file size is about 5-7 MB, but when the file size goes above 25 MB we get exception thrown from the reader.
The exception thrown is - "System.AccessViolationException: Attempted to read or write protected memory."
The code on the C# side is a simple one - we extract data from the database as ref cursor and read the ref cursor using OracleReader. When we try to extract the xml into xmldocument using get reader this is the place where we get the System.AccessViolationException while trying to read the huge amount of data.
using (var cur= (OracleRefCursor)cmd.Parameters["cur_xml"].Value)
{
if (!cur.IsNull)
{
OracleDataReader rdr= cur.GetDataReader();
while (rdr.Read())
{
XmlDocument x = new XmlDocument();
x.LoadXml(rdr.GetString(0));//this line above throws the System.AccessViolationException
}
}
}
Any suggestion to fix this for large data.
Thanks to all who replied with suggestions to resolve the issue.
However, with some research here and there I was able to resolve the issue myself.
Since we are using Oracle DB, we were referring Oracle.DataAccess in the DAL layer.
I switched the reference from Oracle.DataAccess to Oracle.ManagedDataAccess and with that I was able to resolve the issue and no matter how big the XML we retrieve from the DB(extracted about 35-40 MB XML file) it is not throwing the "System.AccessViolationException: Attempted to read or write protected memory." issue(for now).
I don't know if this solution will work for everyone as they need to first find out the root cause for the error and take action accordingly. For me whenever I tried to extract the details from reader using reader.GetString(0), it threw the exception.
Hope this will be helpful for anyone facing similar issue like me.
Thanks!

System.InvalidOperationException: 'There is an error in XML document (0, 0).'

I am trying to deserialize a file, none of the other solutions are working for me.
This is the code. I get the error on the 'customerList' line
using (StreamReader customerStreamReader =
new StreamReader(#"C:\...\ShoppingApplication\bin\Debug\Customer.xml"))
{
customerList = (List<Customer>)customerSerializer.Deserialize(customerStreamReader);
}
Look into using XDocument instead for it will be more robust in reporting errors, though the 0,0 location is a common one. Avoid using streams because they are so, .Net 2.
Here is an example:
var doc = XDocument.Load(#"C:\...\ShoppingApplication\bin\Debug\Customer.xml");
Console.WriteLine(doc);
Then extract what is needed from the actual nodes.
For anybody coming here from google:
If you do not want to use XDocument, then you must make sure that your .xml is NOT empty. Once I added something, I was able to deserialize it just fine. Hope this helps!

Xamarin Android throws IOException when using ZipFile.ExtractToDirectory method

I try to use the following code in my project without any success and it's driving me mad.
System.IO.Compression.ZipFile.ExtractToDirectory(filePath, appPath);
The parameters are:
filePath = "/storage/emulated/0/Flashback_Backup/memory_backup.zip"
appPath = "/storage/emulated/0/Flashback"
According to the documentation here IOException should be thrown if:
The directory specified by destinationDirectoryName already exists.
-or- The name of an entry in the archive is Empty, contains only white space, or contains at least one invalid character.
-or- Extracting an archive entry would create a file that is outside the directory specified by destinationDirectoryName. (For example,
this might happen if the entry name contains parent directory
accessors.)
-or- An archive entry to extract has the same name as an entry that has already been extracted from the same archive.
As far as I know none of it applies. The zip file is a totally valid one, which I compressed with the Directory.CreateDirectory method, and only contains a few uniquely named JSON files. I tried with and without existing "Flashback" folder too, but nothing seems to work.
If anyone have any ideas or solutions please tell me because I'm seriously lost at this. I can provide more info if needed.
Try to wrap extraction intro try-catch, it may give you a better understanding of what is going on.
try {
System.IO.Compression.ZipFile.ExtractToDirectory(filePath, appPath);
} catch (Exception ex) {
Console.Log(ex);
}
If there is an error, it will be one of your listed above.

C# CsvHelper - Missing CsvReader.cs

I am using CsvHelper to read a .CSV file into an html table. I am using .NET MVC 4 and CsvReader 2.13.2.0. I have tried different versions of CsvHelper and am still getting the same error on this line:
var csv = new CsvReader(new StreamReader(path));
Error:
I've looked in the CsvHelper folder and there is just a .dll, debug database, and an XML document. Does anybody know why this file does not exist or if I am using a depreciated CsvHelper method or something. Thanks!
I know this doesn't directly answer your question but I would look into LinqToCSV (you can find it on Nuget). With it you can use it to serialize a csv file into an object and use linq to iterate over it. It's very easy to use and since it's a package you won't need to worry about the dll management or the code being deprecated. I've used it in the past, I dont remember the exact syntax but loading the file is something like this:
var cc = new CsvContext();
cc.Read<YourModel>(filePath);
That will read the csv file into your C# object. Then you can just use it like any other object.
I had the same problem. But I forgot surrounding it with a try-/catch to see the root exception :-). Simple!
try
{
var csv = new CsvReader(new StreamReader(path));
}
catch (UnauthorizedAccessException e)
{
//handle it
}
catch (System.Exception e)
{
//handle it
}

Load Xdocument with RSS feed in MVVM app

I am learning MVVM & Linq to xml by converting a simple wpf RSS app. Originally, I used XmlDataProvider to load local xml files or RSS urls. I am now using the code below which works fine loading local xml files, but throws the "FileNotFoundException" when its a url. During debugging I see the correct url address in (string RSS_URL), yet the file is not found. My initial searching led me to Webclient & HttpWebRequest, but I haven't had any success with them. Am I on the right track? Any code or tutorial links available?
public static List<RSSItem> Load(string RSS_URL)
{
if (!File.Exists(RSS_URL))
{
throw new FileNotFoundException("Datasource file could not be found", RSS_URL);
}
var rssfiles = XDocument.Load(RSS_URL).Descendants("item").Select(
x => new RSSItem(
(string)x.Element("title"),
(string)x.Element("link"),
(string)x.Element("description")));
return rssfiles.ToList();
}
Thank You
XDocument.Load() will accept URLs without any problem. The issue in your code is that you're using File.Exists() to determine whether or not he URL is valid. File.Exists() only accepts a filesystem path, not a uri.
Quick piece of additional info: the Load() method relies on an underlying XmlReader and a call to Create(). If the resource (the URL in this case) doesn't exist, a WebException will be thrown indicating that the resource doesn't exist.
XDocument.Load info:
http://msdn.microsoft.com/en-us/library/bb343181.aspx
XmlReader.Create info:
http://msdn.microsoft.com/en-us/library/w8k674bf.aspx
The XDocument.Load overload you are using is specifically for loading from a file. You would have to download from the RSS feed to get the XML data locally. Look at the MSDN document here for alternatives such as reading from a stream, which might be better suited to what you need.

Categories