lmoelleb - Viewing Profile: Posts - Netduino Forums
   
Netduino home hardware projects downloads community

Jump to content


The Netduino forums have been replaced by new forums at community.wildernesslabs.co. This site has been preserved for archival purposes only and the ability to make new accounts or posts has been turned off.

lmoelleb

Member Since 12 Aug 2013
Offline Last Active Nov 30 2014 09:13 PM
-----

Posts I've Made

In Topic: Get parts of xml stream from web

16 October 2013 - 05:45 PM

I am quite new to the Netduino coming from the full .NET Framework, but I assume the same approach to large files will work here (I rely on it for my own projects and will need to do some new planning if it isn't the case):

 

Once you get the WebResponse from WebRequest.GetResponse, call GetResponseStream on it.

 

This will give you a stream where the data send from the web server can be read in smaller chunks. You can't directly skip anything, but you can for example read up to 128 bytes at the time into a static buffer, and then not use the data for anything - do that 8 times and you have skipped 1024 bytes. Once you do not want to read more, just close the stream.

 

One thing you need to notice when reading streams (and potentially the problem you are describing here) is that the length you pass into Stream.Read indicates the maximum number of bytes that will be read from the stream. Read will return the actual number of bytes it read, so if you need to read more than you get, you need to call Read again. Notice a return value of 0 always indicate that no more bytes can be read from the stream. A value less than the length you passed in but larger than 0 might mean that there are no more bytes, but it is not something you can rely on. You need to keep calling Read until it returns 0 to read the entire stream.

 

For XML you would typically not do the stream operations yourself. Instead you will use a forward only XML reader. This will ´"walk" through the XML document letting your program know when it encounters elements and attributes. You can then store the values you need as they are encountered without having to load the entire XML document into memory at once. System.Xml.XmlReader.Create(stream) will create the reader for your response stream. Notice I did not try this one yet - but it compiles so I assume it is present on the NetDuino 2.  

For large JSON objects you should do something similar. I wrote my own JSON forward only reader, but maybe there is something out there you can use as well if needed.  


home    hardware    projects    downloads    community    where to buy    contact Copyright © 2016 Wilderness Labs Inc.  |  Legal   |   CC BY-SA
This webpage is licensed under a Creative Commons Attribution-ShareAlike License.