c# - xml中的额外右括号

标签 c# xml xmlserializer

我正在使用此示例将一些变量保存到 xml 文件中:

how do I set the current class to the return types results

这是我的设置文件代码:

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>>

最后看到两个 >>

有人知道为什么它在我的 xml 文件末尾保存两个 >>> 吗?

我的代码出错了:

最佳答案

这是因为你正在使用 File.OpenWrite :

For an existing file, it does not append the new text to the existing text. Instead, it overwrites the existing characters with the new characters. If you overwrite a longer string (such as “This is a test of the OpenWrite method”) with a shorter string (such as “Second run”), the file will contain a mix of the strings (“Second runtest of the OpenWrite method”).

虽然从您的示例中看不清楚,但我怀疑新内容比旧内容短一个字节,因此您看到的是原始文件中的右尖括号。

我怀疑你应该只使用 File.Create相反。

关于c# - xml中的额外右括号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14654527/

相关文章:

c# - 动态文本 block

java - 使用 NodeList 遍历 XML 中的所有元素

C# XmlSerializer 使用不同的命名空间序列化同一个类

javascript - 从 JavaScript 访问 List 类的扩展属性

C# 检查 Telegram 登录 hmac

java - 检查相同的文本是否出现在 2 个不同的 xpath 下

c# - 序列化包含 List<Object> 的类

c# - 如何使用asp.net登录其他网站?

C# - 从 HttpWebResponse 接收奇怪的字符

使用抽象类的 C# XML 序列化