Click here to Skip to main content
15,887,435 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I wrote this code in windows phone application to read and write data from xml file and it is work fine.
so i want to use it at windows 8.1 application but it's not work, how i convert it to be compatible with windows 8.1

public void Read(string strXMLFile)
       {
           IsolatedStorageFile isfData = IsolatedStorageFile.GetUserStoreForApplication();
           XDocument doc = null;
           IsolatedStorageFileStream isfStream = null;
           if (isfData.FileExists(strXMLFile))
           {
               isfStream = new IsolatedStorageFileStream(strXMLFile, FileMode.Open, isfData);
               doc = XDocument.Load(isfStream);
               isfStream.Close();
           }
           else
           {
               doc = XDocument.Load(strXMLFile);
               isfStream = new IsolatedStorageFileStream(strXMLFile, FileMode.CreateNew, isfData);
               doc.Save(isfStream);
               isfStream.Close();
           }

           var vData = from s in doc.Descendants("Row") select new ColData(s);
       }


       public void Write(string strXMLFile)
       {
           XElement xml = new XElement("Tabel",
                           from p in this
                           select p.Information);

           IsolatedStorageFileStream isfStream = new IsolatedStorageFileStream(strXMLFile, FileMode.Open, IsolatedStorageFile.GetUserStoreForApplication());
           xml.Save(isfStream);
           isfStream.Close();

       }
Posted

1 solution

check with below methods, if you have setup development environment in windows 8 PC, you better debug line by line and check what is going wrong.
To write a file:
C#
public async Task WriteFile(string fileName, string text)
{
    IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;

    IStorageFile storageFile = await applicationFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);

    using (Stream stream = await storageFile.OpenStreamForWriteAsync())
    {
        byte[] content = Encoding.UTF8.GetBytes(text);
        await stream.WriteAsync(content, 0, content.Length);
    }
}

To read a file:
C#
public async Task<string> ReadFile(string fileName)
{
    string text;
    IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder;

    IStorageFile storageFile = await applicationFolder.GetFileAsync(fileName);

    IRandomAccessStream accessStream = await storageFile.OpenReadAsync();

    using (Stream stream = accessStream.AsStreamForRead((int)accessStream.Size))
    {
        byte[] content = new byte[stream.Length];
        await stream.ReadAsync(content, 0, (int) stream.Length);

        text = Encoding.UTF8.GetString(content, 0, content.Length);
    }

    return text;
}</string>

Ref : Windows Phone 8 Shared Core With Windows 8 – File IO[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900