I am using the code from MSDN documentation, to load the JSON file inside my Application Project. I wanted to use JsonObject to get the contents from the .json file.
Here is the code I am using,
Stream fs = File.Open(
#"C:\Users\AfzaalAhmad\Documents\The VS Org\Personal Diary\events.json",
FileMode.Open);
JsonObject jsonObject = (JsonObject)JsonObject.Load(fs);
But when it is executed it gives me the following error. Please note that the file is Empty.
System.FormatException
The snapshot for the error is as
In the documents I read that the .Load() method takes a Parameter of System.IO.Stream type, the code shows same return type for the fs. But when it is executed it gives me the error. What should I be doing, in order to correct this error?
The exception of System.FormatException gets thrown when there was error converting data and constructing an object.
http://msdn.microsoft.com/en-us/library/system.formatexception.aspx
In my file, the error was that the File was initially empty. Not even a single object was there, which would be a possible JSON object. So, the File content was not perfect fit for the JSON Standards. Thus the Exception got thrown.
I changed that, and used a simple, if else block to check for the contents. It was fixed this way.
Related
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!
I have an embedded resource containing a json file that has 19000 odd Australian suburbs. I have it in a related assembly in my solution build.
My issue is that when I check the variable "resourceStream" after getting the resource it has timed out..
The actual error close up:
resourceStream.ReadTimeout = 'resourceStream.ReadTimeout' threw an exception of type 'System.InvalidOperationException'
Here is the code:
using (var resourceStream = this.GetType().Assembly.GetManifestResourceStream("JobsLedger.INITIALISATION.SUBURB.Initialisations.SuburbJSON.australianSuburbs.json"))
{
var reader = new StreamReader(resourceStream, Encoding.UTF8);
var jsonString = reader.ReadToEnd();
var suburbs = JsonConvert.DeserializeObject<List<BaseSuburb>>(jsonString);
}
So it gets 4900 odd postcodes before timing out.. should get 19000.
The ones it gets are converted and work.. nice, but I actually need all of them :(
There must be a way to stop it from timing out. I noticed a post suggesting changing it to async but there are no await methods, it said, for GetManifestResourceStream.
Also this is on the resource stream.. not on the reader.. I think the reader would be fine to handle 18k records.
How do I get the "resourceStream" to run and actually get the whole file before timing out? or is this a problem with the file?
I'm trying to fix a bug where the following code results in a 0 byte file on S3, and no error message.
This code feeds in a Stream (from the poorly-named FileUpload4) which contains an image and the desired image path (from a database wrapper object) to Amazon's S3, but the file itself is never uploaded.
CloudUtils.UploadAssetToCloud(FileUpload4.FileContent, ((ImageContent)auxSRC.Content).PhysicalLocationUrl);
ContentWrapper.SaveOrUpdateAuxiliarySalesRoleContent(auxSRC);
The second line simply saves the database object which stores information about the (supposedly) uploaded picture. This save is going through, demonstrating that the above line runs without error.
The first line above calls in to this method, after retrieving an appropriate bucketname:
public static bool UploadAssetToCloud(Stream asset, string path, string bucketName, AssetSecurity security = AssetSecurity.PublicRead)
{
TransferUtility txferUtil;
S3CannedACL ACL = GetS3ACL(security);
using (txferUtil = new Amazon.S3.Transfer.TransferUtility(AWSKEY, AWSSECRETKEY))
{
TransferUtilityUploadRequest request = new TransferUtilityUploadRequest()
.WithBucketName(bucketName)
.WithTimeout(TWO_MINUTES)
.WithCannedACL(ACL)
.WithKey(path);
request.InputStream = asset;
txferUtil.Upload(request);
}
return true;
}
I have made sure that the stream is a good stream - I can save it anywhere else I have permissions for, the bucket exists and the path is fine (the file is created at the destination on S3, it just doesn't get populated with the content of the stream). I'm close to my wits end, here - what am I missing?
EDIT: One of my coworkers pointed out that it would be better to the FileUpload's PostedFile property. I'm now pulling the stream off of that, instead. It still isn't working.
Is the stream positioned correctly? Check asset.Position to make sure the position is set to the beginning of the stream.
asset.Seek(0, SeekOrigin.Begin);
Edit
OK, more guesses (I'm down to guesses, though):
(all of this is assuming that you can still read from your incoming stream just fine "by hand")
Just for testing, try one of the simpler Upload methods on the TransferUtility -- maybe one that just takes a file path string. If that works, then maybe there are additional properties to set on the UploadRequest object.
If you hook the UploadProgressEvent on the UploadRequest object, do you get any additional clues to what's going wrong?
I noticed that the UploadRequest's api includes both an InputStream property, and a WithInputStream fluent API. Maybe there's a bug with setting InputStream? Maybe try using the .WithInputStream API instead
Which Stream are you using ? Does the stream you are using, support mark() and reset() method.
Might be while upload method first calculate the MD5 for the given stream and then upload it, So if you stream is not supporting these two method then at the time of MD5 calculation it reaches at eof and then unable to preposition for the stream to upload the object.
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.
Im trying to deserialize an XML file with XmlSerializer, however im getting this exception:
"There is an error in XML document (1,
2)" The innerexception is:
"<Mymessage
xmlns='http://MyMessages/'> was not
expected."
Which is the very first line in the XML file. my guess is that it has something to do with the xmlns.
I tried to ask Google, and then tried to add the following line to my code
[XmlRoot("MyMessage", Namespace="'http://MyMessages/")]
But I still get the same exception.
In the constructor of the XmlSerializer i needed to specify a default namespace, after doing that everything worked just fine
Please provide the full XML file code to help understand the issue better.
Also put this as the first line in the xml file and see if this solves the issue
<?xml version="1.0" encoding="utf-8"?>
Further to CruelIO's response, I resolved the error by adding:
[XmlRoot("RenderResult", Namespace = "http://mynamespace.uri.org")]
to the class that I was trying to deserialize. e.g: the serialization code was:
RenderResult result;
using (var memoryStream = new MemoryStream(data))
{
var xmlSerializer = new XmlSerializer(typeof(RenderResult));
result = (RenderResult)xmlSerializer.Deserialize(memoryStream);
}
and my class looked like this:
[XmlRoot("RenderResult", Namespace = "http://mynamespace.uri.org")]
public class RenderResult
{
}
It sounds like you have a borked xml file. Easy ways to find out:
try loading it into an xml viewer
or just make sure it has a .xml extension and load in VS or IE
or run xsd.exe over it
If they complain, then the xml is certainly corrupt.
If they work fine, and display your data, then you probably have the serialization attributes wrong. Try using xsd.exe with the "/classes" switch to see what it would do with it...