I’m attempting to develop a Windows 7 Phone and I am using an XML file that I need to parse and then perform a Linq query on.
The problem is this:
Whenever I try to access the file (it is stored locally) it brings back an error saying the file cannot be found as it’s not part of the XAP package.
I have tried another solution where I use StreamReader But I am still getting a simular error:
Attempt to access the method failed System.IO.File.OpenText(System.String)
Here is the code that I am using:
using (StreamReader reader = File.OpenText("C:/Users/Desktop/Assign/obj/Debug/buildings.kml"))
{
var xdoc = XDocument.Load ("buildings.kml");
XNamespace kml = "http://www.opengis.net/kml/2.2";
var dict = xdoc.Descendants(kml + "Placemark")
.ToDictionary(d => d.Element(kml + "name").Value,
d => d.Element(kml + "id").Value);
foreach (var b in dict) {
Console.WriteLine ("Building Name -> " + b.Key + " Building ID -> " + b.Value);
}
}
The file is located in: > C:/Users/Desktop/Assign/obj/Debug/buildings.kml so I cannot see the problem. Outside of Visual Studio, I can read in the .xml file fine.
Hope someone can help
EDIT:
New code –
Dictionary<string, string> getBuildingNames()
{
Uri uri = new Uri(@"Data\mydata.kml", UriKind.Relative);
StreamResourceInfo sri = Application.GetResourceStream(uri);
StreamReader sr = new StreamReader(sri.Stream);
var xdoc = XDocument.Load(sr);
XNamespace kml = "http://www.opengis.net/kml/2.2";
var dict = xdoc.Descendants(kml + "Placemark")
.ToDictionary(d => d.Element(kml + "name").Value,
d => d.Element(kml + "id").Value);
return dict;
}
Error: – ‘NullReferenceException was unhanded’
Assuming you really are trying to do this as part of a WP7 project (rather than some non-mobile project related to it, e.g. preprocessing) you shouldn’t be using
File.OpenText.Options:
Application.GetResourceStream(see this blog post for details)Assembly.GetManifestResourceStream.