Parse csv or excel file in Silverlight
I've parsed these files in regular C# applications, but the IO methods for the files are different in Silverlight, and I can't seem to find the right methods. Searches haven't turned up any information I can use. For the real application I'll be receiving开发者_如何学编程 XML from the server, but for the prototype I just need to parse a file with some sample data in it.
You can save the Excel file as XML. An example can be found in this link
This way you can keep your import procedure the same and process the data as when you go live.
To access files from the user's machine you are required to use the OpenFileDialog and SaveFileDialog. Without elevated trust (requires out of browser apps) you will not be able to know anything more than the filename the user selected for input/saving; you will have no idea what the path is to this file. This function can only be called as the result of a user taking an action such as clicking a button; otherwise it will fail because Silverlight does not want malicious code prompting a user with annoying dialogs automatically.
To do this you would do something as follows:
var openFile = new OpenFileDialog();
if ( open.ShowDialog() == true ) // Sadly this is a nullable bool so this is necessary
{
using( Stream myStream = openFile.File.OpenRead() )
using ( var reader = new StreamReader( myStream ))
{
...
}
}
精彩评论