xml 中的额外右括号

2024-01-08

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

如何将当前类设置为返回类型结果 https://stackoverflow.com/questions/14639554/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 http://msdn.microsoft.com/en-us/library/system.io.file.openwrite.aspx:

对于现有文件,它不会将新文本附加到现有文本。相反,它会用新字符覆盖现有字符。如果您使用较短的字符串(例如“Second run”)覆盖较长的字符串(例如“This is a test of the OpenWrite method”),则该文件将包含字符串的混合(“Second runtest of the OpenWrite method”) ”)。

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

我怀疑你应该使用File.Create http://msdn.microsoft.com/en-us/library/system.io.file.create.aspx反而。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

xml 中的额外右括号 的相关文章

随机推荐

  • 如何应用KineticJS过滤器?

    我发现 KineticJS 滤镜文档非常令人沮丧 并且无法在网上找到示例 尤其是考虑到亮度 Kinetic filters 文档link http kineticjs com docs symbols Kinetic Filters php
  • 单击球体

    我有一个以正交投影为中心绘制的单位球体 半径为 1 球体可以自由旋转 如何确定用户单击球体上的点 Given 显示器的高度和宽度 投影圆的半径 以像素为单位 用户点击点的坐标 假设左上角为 0 0 则 x 值随着向右移动而增加 y 值随着向
  • 为什么 Grpc.Core NuGet 包这么大?

    最新的 Grpc Core NuGet 包有 150 MB 之大 它似乎在其一生中稳步增长 这里有一些例子 v1 17 1 2018 年 12 月 48 34 MB v2 23 1 2019 年 8 月 68 11 MB v2 30 0 2
  • 如何制作自定义 Java/JavaFX 控制台?

    有必要制作一个自定义控制台 我有以下代码 public class Console extends OutputStream private Console ResourceBundle resourceBundle throws IOEx
  • 抑制来自套接字的“不允许操作”错误

    我有一个小型实用程序 可以处理 TCP 和 UDP 套接字 有时 我会在 stderr 中打印错误 不允许操作 我的问题是 我根据套接字函数的返回码 偶尔是 errno 处理错误 并且不打印任何内容 因此 该消息必定来自套接字调用之一 我不
  • 如何在 SQL Server 中检查 select 查询结果是否为 NULL

    在 SQL Server 中 如何验证查询是否返回 NULL 并根据它运行块 例如在查询 1 中 我想检查if count is not null然后检查它是否 gt 0 我应该使用if exists here if select coun
  • CSS 样式表在顶部还是底部?或者如何解决空白页问题?

    我一直将样式表放在顶部 之间 的html 据我了解 这是最佳实践 例如 http stevesouders com hpws css bottom php http stevesouders com hpws css bottom php
  • Python:将 IP 地址打包为 ctype.c_ulong() 以与 DLL 一起使用

    给出以下代码 import ctypes ip 192 168 1 1 thisdll ctypes cdll aDLL thisdll functionThatExpectsAnIP ip 我怎样才能正确地将它打包为一个期望它作为 c u
  • 最大数量数组中总和小于或等于 k ​​的元素的数量

    我想找到最大的数 给定正整数数组中的元素的总和小于或等于给定的编号 k 例如 我有一个数组 3 4 7 2 6 5 1 and k 6 答案是 3 因为 1 2 3 是得出总和 6 的最大元素 对数组进行排序 计算元素的数量 然后开始按顺序
  • 单击按钮后如何关闭弹出模式

    选中复选框然后单击按钮后如何关闭弹出模式 如果未选中复选框 则不关闭模式 复选框和按钮放置在模态内 当我检查复选框并单击按钮时 模式不会关闭 document ready function one checked on click clos
  • 8086 汇编语言中的换行符:我的文本打印为阶梯式

    I m getting stair step output like this 我的程序工作正常 除了当我打印一个新行 然后打印当前行中的某些内容时 它会显示在下一行上 但前面有一些空格 程序是这样的 打印0到9的多个数字的表格 data
  • localhost 和 0.0.0.0 的 IPV6 是什么?

    众所周知 IPv4 地址localhost is 127 0 0 1 环回地址 IPv6 地址有什么用途localhost并为0 0 0 0因为我需要阻止一些广告主机 众所周知 IPv4 地址localhost is 127 0 0 1 环
  • 列表中的每个孩子都应该有唯一的“key”道具

    我不断收到此警告 列表中的每个孩子都应该具有唯一的 key 道具 即使我有具有不同键的独特项目 每当我创建一个新的 植物 对象时 我都会给它一个新的 uuid setPlants prevItems gt return name newPl
  • hadoop 集群应该在相同的硬件上运行吗?

    我记得在某处读到过 如果运行 Hadoop 的机器彼此之间差异很大 那么 Hadoop 的性能会显着下降 但我似乎找不到该评论了 我正在考虑在不由我的团队直接管理的虚拟机阵列上运行 Hadoop 集群 我需要知道这是否是我应该在请求中提出的
  • 如何在 Xcode 10 中打开 Xcode 11 Beta 4 项目?

    我在 Xcode 11 Beta 4 中开发了一个项目 然后降级到 Xcode 10 并且无法打开该项目 我想将我的应用程序提交到应用程序商店 但我在 Xcode 11 beta 4 中不断收到错误消息 我是初学者 我知道我篡改了构建设置
  • 在 Unity3D 中,“设置”网格的边界会做什么或实现什么?

    在 Unity 代码库中 我看到了这个 the game object currently has no mesh attached MeshFilter mFilter gameObject AddComponent
  • 我们可以使用 for-each 循环遍历传递给函数的数组吗?

    我知道我们可以通过这种方式迭代作为参数传递的数组 NO ERROR void fun int a int n for int i 0 i
  • 如何在 NetBeans 中显示/显示隐藏或不可见的字符?

    如何在 NetBeans 中显示 显示隐藏字符 在其他编辑器中 如果打开此功能 空格可能会显示为小中心点 制表符可能会显示为右箭头 此功能对于查看文件是否使用制表符或空格进行缩进等很有用 这个功能已经缺失很长一段时间了 功能要求 https
  • Apache Camel 中的忽略消息

    希望这听起来并不荒谬 但我怎样才能丢弃消息在骆驼故意 到目前为止 我将它们发送到日志组件 但同时我什至不想记录提款 有没有 dev 空骆驼的端点 您可以使用消息过滤器 eip 来过滤掉不需要的消息 http camel apache org
  • xml 中的额外右括号

    我使用此示例将一些变量保存到 xml 文件 如何将当前类设置为返回类型结果 https stackoverflow com questions 14639554 how do i set the current class to the r