如何创建XML文件到某个文件夹?

时间:2014-01-12 11:35:25

标签: c# silverlight windows-phone-8

如何将XML文件创建到某个文件夹?

isoStream = new IsolatedStorageFileStream("**Folder/XmlFile.xml**", FileMode.Create, isoStore);

2 个答案:

答案 0 :(得分:2)

直接从Quickstart: Working with files and folders in Windows Phone 8

抓取

查看“创建文件夹并写入文本文件”部分

private async void btnWrite_Click(object sender, RoutedEventArgs e)
{
    await WriteToFile();

    // Update UI.
    this.btnWrite.IsEnabled = false;
    this.btnRead.IsEnabled = true;
}

private async Task WriteToFile()
{
    // Get the text data from the textbox. 
    byte[] fileBytes = System.Text.Encoding.UTF8.GetBytes(this.textBox1.Text.ToCharArray());

    // Get the local folder.
    StorageFolder local = Windows.Storage.ApplicationData.Current.LocalFolder;

    // Create a new folder name DataFolder.
    var dataFolder = await local.CreateFolderAsync("DataFolder",
    CreationCollisionOption.OpenIfExists);

    // Create a new file named DataFile.txt.
    var file = await dataFolder.CreateFileAsync("DataFile.txt",
    CreationCollisionOption.ReplaceExisting);

    // Write the data from the textbox.
    using (var s = await file.OpenStreamForWriteAsync())
    {
        s.Write(fileBytes, 0, fileBytes.Length);
    }
}

答案 1 :(得分:1)

下面的函数会将Jagged数组double [] []保存到XML。您可以通过修改自己的数据类型来使用它:

   private void Save(double[][] m, string filePath)
    {
        //Open a file stream 
        System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Create);
        // Create a xml Serializer object
        System.Xml.Serialization.XmlSerializer xmlSer = new System.Xml.Serialization.XmlSerializer(typeof(double[][]));

        xmlSer.Serialize(fs, m);
        // Close the file stream
        fs.Close();         
    }

而不是double [][] m你应该把自己的类型。