Open local XML file in WP7

I have a local xml file and need to upload it to isolated storage with my application. starts for the first time. but if I open this file using the stream OR File.Open, it throws an error. I also need to save this file in serialized form. Please help, like new to WP7 and C #, not able to crack it.

To open the XML file, use the following code:

FileStream stream = new FileStream("site.xml", FileMode.Open); 
+1
source share
2 answers

If the file is part of your project, you can add the file to your project as a resource. This means that the Build Action file must be Resource . And the next function can read the file from the project resource.

 Stream stream = this.GetType().Assembly.GetManifestResourceStream("Namespace.FileName.FileExtentation"); 

Now you can read the stream according to the file type. If it is an XML file, you can read it as shown below.

As an example

 XElement element = XElement.Load(stream); foreach (XElement phraseElement in element.Elements()) { MyClass foo = new foo(); foreach (XAttribute attribute in phraseElement.Attributes()) foo.Text = attribute.Value; } 

How to write isolated storage and read a file from isolated storage? Please read the following link

http://www.codebadger.com/blog/post/2010/09/03/Using-Isolated-Storage-on-Windows-Phone-7.aspx

You can also check the link below regarding the local data store for Windows Phone.

http://msdn.microsoft.com/en-us/library/ff626522%28v=vs.92%29.aspx

+2
source

You also need to specify the FileAccess mode for any resource such as content, since you cannot open it in write mode.

Try changing your code to:

 FileStream stream = new FileStream("site.xml", FileMode.Open, FileAccess.Read); 

Also see this question: How to fix MethodAccessException while reading a file?

+1
source

Source: https://habr.com/ru/post/914377/


All Articles