5

この例を使用して、いくつかの変数を xml ファイルに保存しています。

現在のクラスを戻り値の型の結果に設定する方法

これは設定ファイルの私のコードです:

using System;
using System.IO;
using System.Xml.Serialization;

namespace ssscc.Settings
{
  public class AppSettings
  {
    public string ReceiptLine1 { set; get; }
    public string ReceiptLine2 { set; get; }
    public string ReceiptLine3 { set; get; }
    public string ReceiptLine4 { set; get; }
    public string ReceiptLine5 { set; get; }
    public string ReceiptLine6 { set; get; }
    public bool ReceiptLine1Enabled { set; get; }
    public bool ReceiptLine2Enabled { set; get; }
    public bool ReceiptLine3Enabled { set; get; }
    public bool ReceiptLine4Enabled { set; get; }
    public bool ReceiptLine5Enabled { set; get; }
    public bool ReceiptLine6Enabled { set; get; }

    public string GatewayUserName { set; get; }
    public string GatewayPassword { set; get; }
    public string GatewayId { set; get; }

    private static string GetSettingsFile()
    {
      var exePath = System.Windows.Forms.Application.StartupPath;
      var sharedDirectory = Path.Combine(exePath, "shared");
      var settingsDirectory = Path.Combine(sharedDirectory, "settings");
      var settingsFile = Path.Combine(settingsDirectory, "ssscc.xml");

      if (!Directory.Exists(sharedDirectory))
      {
        Directory.CreateDirectory(sharedDirectory);
      }

      if (!Directory.Exists(settingsDirectory))
      {
        Directory.CreateDirectory(settingsDirectory);
      }

      return settingsFile;
    }

    internal void SaveSettings()
    {
      var serializer = new XmlSerializer(typeof(AppSettings));
      using (var stream = File.OpenWrite(GetSettingsFile()))
        serializer.Serialize((Stream)stream, this);
    }

    internal static AppSettings GetInstance()
    {
      try
      {

      if (!File.Exists(GetSettingsFile()))
        return null;

      var serializer = new XmlSerializer(typeof(AppSettings));
      using (var stream = File.OpenRead(GetSettingsFile()))
      {
        return (AppSettings)serializer.Deserialize(stream);
      }

      }
      catch (Exception ex)
      {
        Console.WriteLine(ex.Message);
        throw;
      }
    }

  }
}

データを保存すると、最初の保存はうまくいき、ファイルの最後に次のように表示されます。

<?xml version="1.0"?>
<AppSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ReceiptLine1 />
  <ReceiptLine2 />
  <ReceiptLine3 />
  <ReceiptLine4 />
  <ReceiptLine5 />
  <ReceiptLine6 />
  <ReceiptLine1Enabled>false</ReceiptLine1Enabled>
  <ReceiptLine2Enabled>true</ReceiptLine2Enabled>
  <ReceiptLine3Enabled>false</ReceiptLine3Enabled>
  <ReceiptLine4Enabled>false</ReceiptLine4Enabled>
  <ReceiptLine5Enabled>false</ReceiptLine5Enabled>
  <ReceiptLine6Enabled>false</ReceiptLine6Enabled>
  <GatewayUserName>asdfasdf</GatewayUserName>
  <GatewayPassword>asdf</GatewayPassword>
  <GatewayId>sdf</GatewayId>
</AppSettings>

ファイルを更新して再度保存すると、次のようになります。

<?xml version="1.0"?>
<AppSettings xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <ReceiptLine1 />
  <ReceiptLine2 />
  <ReceiptLine3 />
  <ReceiptLine4 />
  <ReceiptLine5 />
  <ReceiptLine6 />
  <ReceiptLine1Enabled>false</ReceiptLine1Enabled>
  <ReceiptLine2Enabled>true</ReceiptLine2Enabled>
  <ReceiptLine3Enabled>false</ReceiptLine3Enabled>
  <ReceiptLine4Enabled>false</ReceiptLine4Enabled>
  <ReceiptLine5Enabled>false</ReceiptLine5Enabled>
  <ReceiptLine6Enabled>false</ReceiptLine6Enabled>
  <GatewayUserName>asdfasdf</GatewayUserName>
  <GatewayPassword>asdf</GatewayPassword>
  <GatewayId>sdf</GatewayId>
</AppSettings>>

最後に2つ見え>>ます。

>>私のxmlファイルの最後に2つ保存されている理由は誰にも分かりますか?

そして、私のコードは次のようにエラーになります:

4

1 に答える 1

11

それはあなたが使用しているためですFile.OpenWrite

既存のファイルの場合、新しいテキストは既存のテキストに追加されません。代わりに、既存の文字を新しい文字で上書きします。長い文字列 (「This is a test of the OpenWrite method」など) を短い文字列 (「Second runtest」など) で上書きすると、ファイルには文字列が混在して含まれます (「Second runtest of the OpenWrite method」など)。 」)。

あなたの例からは明らかではありませんが、新しい内容は古い内容よりも 1 バイト短いと思われるため、元のファイルの閉じ山かっこが表示されています。

代わりに使用する必要があると思いますFile.Create

于 2013-02-01T20:36:05.790 に答える