2014-11-08 44 views
0

我在windows phone應用程序中編寫了此代碼以便從xml文件讀取和寫入數據,並且工作正常。 ,所以我想在Windows 8.1中的應用程序使用它,但它沒有工作,我如何將其轉換爲與Windows 8.1將IsolatedStorageFileStream從Windows phone 8.1轉換爲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(); 

    } 
+0

你究竟是什麼意思,但「不起作用」? – 2014-11-09 13:25:23

+0

我的意思是這個代碼適用於Windows Phone 8.1應用程序,但不適用於Windows 8.1應用程序或Windows RT應用程序 – masalahi 2014-11-09 13:32:45

+0

是的,但是定義什麼不起作用。拋出異常?代碼掛起?究竟是什麼? – 2014-11-09 13:34:26

回答

0

您需要使用新的API,以讀取文件兼容

var file = await localFolder.GetFileAsync("dataFile.txt"); 
var data = await FileIO.ReadTextAsync(file); 

和寫入

var file = await localFolder.CreateFileAsync("dataFile.txt",  CreateCollisionOption.ReplaceExisting); 
await FileIO.WriteTextAsync(file , "data"); 
0

成員DamithSL回答在代碼項目選址問題

http://www.codeproject.com/Questions/839861/Convert-IsolatedStorageFileStream-from-Windows-pho

public async Task Read(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); 

     } 

     XDocument loadedDataH = XDocument.Parse(text); 
     var vPosting = from query in loadedDataH.Descendants("Row") 
         select new clssColPost(query); 
     this.Clear(); 
     AddRange(vPosting); 

    } 


    public async Task Write(string fileName) 
    { 

     XElement xml = new XElement("Tabel", 
          from p in this 
          select p.Information); 


     IStorageFolder applicationFolder = ApplicationData.Current.LocalFolder; 

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

     using (Stream stream = await storageFile.OpenStreamForWriteAsync()) 
     { 
      byte[] content = Encoding.UTF8.GetBytes(xml.ToString()); 
      await stream.WriteAsync(content, 0, content.Length); 
     } 
    } 
相關問題