Is Isolated Storage Always Necessary? - windows-phone-7

I see alot of examples on how to write data from an app to a file then put it in isolated storage. I do not want to write any data to my xml file, I just simply want to save it into isolated storage then query it later.
A few simple questions
Someone have code on how to put an existing xml file into isolated storage. Also since I am not writing to this file, do I need isolated storage still? Can I just add the xml to my project and use Linq to xml to open it query it and close it on a button click?
I wanna query the xml through my application in the background. I see alot of examples on serializing, do I need to do this? Can I just open the xml file and use linq to xml to query the data?
Can I just do this, set bbxml.xml to Content and forget about isolated storage and just do this?
using (XmlReader reader = XmlReader.Create("bbxml.xml"))
{
XDocument xml = XDocument.Load(reader);
//query xml....
}

Include the XML file in your project files in Visual Studio, then in the Properties window make sure Build Action is set to Content and Copy to Output Directory is set to Copy always or Copy if newer. This will include the file in the output XAP file.
To access this file in code use:
XDocument doc = XDocument.Load( "path/to/my/file.xml" );
Of course, it doesn't have to be XDocument, you can use any XML reader class similarly.

Related

attaching unit test data to unit tests in visual studio

I heavily make use of unit tests for my developer needs (POCs, unit tests, etc). For one particular test method there was a line that went...
var file = #"D:\data\file.eml";
So I am referencing some file on my file system.
Now in a team when other people are trying to run my "personal" tests (POCs or whatever) they don't have a reference to that file in that path...hence the tests fails. How we'd have to normally make this work is to provide the test data, and allow the user to modify the test code so that it runs on his computer.
Any visual studio way to manage this particular problem?
Whats the benefit in this? Well, people can review the test data (email in my case) as well as the method I wrote for testing, and can raise defects in TFS (the source control system) relating to it if need be.
One way I often handle data files for unit test projects are to set the data files as Resources. (* Note that this link is for vs2010 but I have used this approach through vs2015RC).
In the project with the data file: Project -> Properties -> Resources and choose to add a resource file if you the project doesn't already have one. Select Files in the resource pane and click Add Resource or just drag and drop your data files onto the resource manager. By default resources are marked internal, so to access the resources from another project you have several ways:
In the assembly with the data files, add the following to your AssemblyInfo.cs file and this will allow only specified assemblies to access the internal resources
[assembly: InternalsVisibleTo("NameSpace.Of.Other.Assembly.To.Access.Resources")]
Create a simple provider class to abstract away the entire Resource mechanism, such as:
public static class DataProvider
{
public static string GetDataFile(int dataScenarioId)
{
return Properties.Resources.ResourceManager.GetString(
string.Format("resource_file_name_{0}", id));
}
}
Change the resource management to public (not an approach I have used)You can then access the data file (now a resource) from a unit test such as:
[TestCase(1)]
public void X_Does_Y(int id)
{
//Arrange
var dataAsAString = Assembly_With_DataFile.DataProvider.GetScenario(id);
//Act
var result = classUnderTest.X(dataAsAString);
//Assert
Assert.NotNull(result);
}
Note that using data files as resources, the ResourceManager handles the file I/O and returns strings of the file contents.
Update: The test method in the example above is from an NUnit project and is not meant to imply process, but a mechanism by which a data file can be accessed from another project.
What you'd normally do is add the file to your project and check it into TFS. Then make sure the item's settings are:
Build action: Content
Copy to output: If newer
Then put an attribute on your Test method or Test class:
[DeploymentItem("file.eml")]
You can optionally specify an output dircetory:
[DeploymentItem("file.eml", "Directory to place the item")]
If you put the files in subdirectories of your test project, then adjust the attribute accordingly:
[DeploymentItem(#"testdata\file.eml")]
The file will be copied to the working directory of your test project and that makes it easy to access from your test code. Either load the file directly, or pass the path to any method that needs it.
If you tests expect the files in a specific location you can use a simple System.IO.File.Copy() or System.IO.File.Move() to put the item in the place you need it to be.
The process is explained here on MSDN.
I suppose the most straight forward way is to simply add whatever to the project, and set the correct value for Copy To Output Directory. In other words, say your data is in a text file.
Add text file to your test project
Right-click to access properties window
Set copy to output directory field as Always or Copy if newer.
Now if you build the test project, the file gets copied to your output directly. This enables to write unit test code of the fashion:
var dataFile = File.OpenRead("data.txt");

Maintaining file of object properties in Watin + Visual Studio

I am working with Watin and maintaining excel file for listing object properties.
It takes lot of time to when I read object properties from excel file.
Is there any other way to store these object properties at centralised location so that we can refer object properties and change in any object will get reflected in all TCs using this object.
In short content of excel file is as follow:
Menu----------------------------id
username_txtfield------------uname
password_txtfield------------ pass
ok_webedit-------------------submit
You can use appSettings in App.config for managing key-value pairs - access is very fast.
You'll also want to create a data access class that abstracts away where the settings are actually being stored so that if (when?) you change where you properties are being stored you just change one class rather than going through your entire codebase making changes.

Adding files to WP7 isolated storage from Visual Studio?

I'm working on an Windows Phone 7 app where I'm going to show ATM's nere your location with bing maps.
I have an xml-file with addresses and gps coordinates. But how do I add this file to my program from visual studio? If I set BuildAction to Content and Copy to output directory to Copy always. The file still isn't in IsolatedStorage. Do I have to build a mechanism to download the information from the web? Or is there another way?
Files listed as content in the Visual Studio project are copied to the generated XAP file (which is analogous to a ZIP file). They are not copied to isolated storage.
In the case of an XML file, you can call XmlReader.Create with the path to the file as argument, as follows:
using (XmlReader reader = XmlReader.Create("path/to/file.xml"))
{
// read XML file here
}
Or you can also call Application.GetResourceStream and use the Stream property of the returned StreamResourceInfo object:
StreamResourceInfo sri = Application.GetResourceStream(
new Uri("path/to/file.xml", UriKind.Relative));
// read XML file here from sri.Stream, e.g. using a StreamReader object
You cannot directly pass files to the isolated storage at design time. Only when the application is running.
I'd still recommend passing the file to the application through a web service. Mainly because if eventually you will need to change the contents of the XML, you will need to update the application.
What I would do is simply create a WCF service that will return serialized data (or the existing XML) via a simple HTTP request.
The "Mango" SDK ships with the ISETool that can take and restore snapshots of an application's isolated storage to/from a local directory:
# Copy data from IS to directory
ISETool.exe ts xd <PRODUCT-ID> "C:\TempDirectory\IsolatedStore"
# Copy data from IS to directory
ISETool.exe rs xd <PRODUCT-ID> "C:\TempDirectory\IsolatedStore"
If you don't want to overwrite the entire IS, the tool supports an option (device-folder) for specifying a sub-directory to backup/restore.

converting schema in XML to XSD for visual studio

Note, I am not trying to generate the schema for an XML file from the file. There is plenty about that on the web. Kind of a waste--how can a file fail validation if the validator was generated from the file it's validating?
Anyway, I want to test an SSIS component that requires a configuration file in XML (not XSD) to tell it how to parse another input file (this input is also not XML).
The provider of this component says we have to create our own config xml files. I would like to use Intellisense to help do that. There is a schema for these config files, but it is in XML.
Is there a way to create a schema from an XML file that represents the content of that file, not the structure?
Example is in second paragraph. Anyway, it turns out there is a way, and it's embarassingly simple.
Just rename the file from whatever.xml to whatever.xsd

Programatically retrieve an attachment stored on a note on a CRM 4.0 entity

How would you suggest working with files that is stored on the note of a entity in Crm. Could you write a generic method that will enable you to access any type of file? Or would it be better to have a method for dealing with each type of file?
For example, we are going to be saving a mix of swf files and xml files on the entity, so would it make sense to have a method each for example:
GetXmlFilesOnAccount(accountid)
GetSwfFilesOnAccount(accountid)
When you upload an attachment to CRM the mimetype is also saved as part of the record information.
The following link contains a nice example of how to download the attachemt using a single method. http://crmscape.blogspot.com/2009/10/ms-crm-40-sending-attachments-to.html
The post is missing the actual query needed to retrieve the annotations but you can tell what columns are required from the method signature.
My suggestion using your methods:
* GetXmlFilesOnAccount(accountid)
* GetSwfFilesOnAccount(accountid)
Retrieve account activitypointers by regardingobjectid(in your case accountid guid)
Loop through returned activitypointers
Get attachments for each activitypointer (activitypointer.activityid = activitymimeattachment.activityid)
Store attachments (disk, etc)
You don't even need two methods. You can retrieve all attachment file types for a given note (annotation) with a single method.
Hope this helps.
I recently started an Open Source Project on CodePlex to accomplish exactly that. Feel free to check out the Project's Web Page at:
http://crmattachdownload.codeplex.com/
You can also view the source code under the "Source Code" tab of that same page.
Pete

Resources