在XML额外的右括号

在XML额外的右括号

本文介绍了在XML额外的右括号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用这个例子来一些变量保存到一个XML文件:

I am using this example to save some variables to an xml file:

这是我的设置文件代码:

This is my code for the settings file:

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

  }
}

在我保存的数据,初步保存去罚款,并在文件末尾它表明:

When I save the data the, the initial save goes fine and at the end of the file it shows:

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

当我更新文件并再次保存它,我结束了这一点:

When I update the file and save it again, I end up with this:

<?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文件的结尾?

Anyone see why it's saving two >> at the end of my xml file?

和我的代码错误出有:

推荐答案

这是因为你'重新使用:

It's because you're using File.OpenWrite:

对于现有的文件,它不追加新的文本到现有文本。相反,它会以新的角色现有的字符。如果覆盖更长的字符串(如这是OpenWrite方法的测试),用较短的字符串(如第二轮),该文件将包含字符串的混合物(第二的runTest的OpenWrite方法)。

虽然它不是从你的榜样清楚,我怀疑新的内容是一个字节比旧的内容较短,所以你恭候看到从原始文件的右尖括号。

While it's not clear from your example, I suspect the new contents are one byte shorter than the old contents, so you're seeing the closing angle bracket from the original file.

我怀疑你应该使用的来代替。

I suspect you should just use File.Create instead.

这篇关于在XML额外的右括号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-24 08:21