简体   繁体   English

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

[英]How to create XML file to some folder?

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

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

Snatched directly from the Quickstart: Working with files and folders in Windows Phone 8 直接从快速入门中抢夺:在Windows Phone 8中使用文件和文件夹

Check out the section "Creating a folder and writing to a text file" 查看“创建文件夹并写入文本文件”部分

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);
    }
}

Below function will save the Jagged array double[][] to the XML. 下面的函数会将锯齿状数组double [] []保存到XML。 You may use it by modify to your own data type: 您可以通过修改为自己的数据类型来使用它:

   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();         
    }

Just instead of the double [][] m you should put your own type. 您应该输入自己的类型,而不是double [][] m

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM