Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
有时C#XmlSerializer序列化(保存)错误,字符数超出预期_C#_Xmlserializer - Fatal编程技术网

有时C#XmlSerializer序列化(保存)错误,字符数超出预期

有时C#XmlSerializer序列化(保存)错误,字符数超出预期,c#,xmlserializer,C#,Xmlserializer,通常情况下,您操作的文件工作正常,但有时文件会出现这样的错误。(“结束额外部分”ConfigSetting>”),我所做的是打开、编辑和保存它 <?xml version="1.0"?> <SystemConfigSetting xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" IsRuntime="true"> <Re

通常情况下,您操作的文件工作正常,但有时文件会出现这样的错误。(“结束额外部分”ConfigSetting>”),我所做的是打开、编辑和保存它

<?xml version="1.0"?>
<SystemConfigSetting xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" IsRuntime="true">
<ReportSchedulers>
<ReportScheduler ReportName="report1" FullreportName="report1" IsEnableAutosaveReport="true" Hour="14" Minute="50" Second="0" ReportSave="Daily" DayofWeek="Sunday" LastUpdate="0001-01-01T00:00:00" />
<ReportScheduler ReportName="report2" FullreportName="report2" IsEnableAutosaveReport="false" Hour="0" Minute="0" Second="0" ReportSave="Daily" DayofWeek="Sunday" LastUpdate="0001-01-01T00:00:00" />
<ReportScheduler ReportName="report3" FullreportName="report3" IsEnableAutosaveReport="false" Hour="0" Minute="0" Second="0" ReportSave="Daily" DayofWeek="Sunday" LastUpdate="0001-01-01T00:00:00" />
<ReportScheduler ReportName="report4" FullreportName="report4" IsEnableAutosaveReport="false" Hour="0" Minute="0" Second="0" ReportSave="Daily" DayofWeek="Sunday" LastUpdate="0001-01-01T00:00:00" />
<ReportScheduler ReportName="report5" FullreportName="report5" IsEnableAutosaveReport="false" Hour="0" Minute="0" Second="0" ReportSave="Daily" DayofWeek="Sunday" LastUpdate="0001-01-01T00:00:00" />
</ReportSchedulers>
</SystemConfigSetting>ConfigSetting>
系统配置设置:

public static bool SaveSystemConfig(SystemConfigSetting systemConfig)
    {
        try
        {
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(SystemConfigSetting));
            using (FileStream fs = new FileStream(CONFIGFILEPATH), FileMode.Open))
            {
                xmlSerializer.Serialize(fs, systemConfig);
                fs.Close();
            }
            return true;
        }
        catch (Exception ex)
        {
            return false;
        }
    }
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace HZSpreadSheet.Model
{
    [Serializable]
    [XmlRoot]
    public class SystemConfigSetting
    {
        private bool isRuntime = false;
        private List<ReportScheduler> reportSchedulers = new List<ReportScheduler>();
        [XmlAttribute]
        public bool IsRuntime { get => isRuntime; set => isRuntime = value; }
        [XmlArray]
        [XmlArrayItem]
        public List<ReportScheduler> ReportSchedulers { get => reportSchedulers; set => reportSchedulers = value; }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;

namespace HZSpreadSheet.Model
{
    public enum ReportSaveFrequency
    {
        Daily = 0,
        Weekly = 1,
        Monthly = 2,
        Manually =3
    }
    public enum DayofWeek
    {
        Sunday = 0,
        Monday = 1,
        Tuesday = 2,
        Wednesday = 3,
        Thursday = 4,
        Friday = 5,
        Saturday = 6
    }
    [XmlRoot]
    public class ReportScheduler
    {
        private string abbr_reportName;
        private string fullreportName;
        private bool isEnableAutosaveReport = false;
        private int hour = 0;
        private int minute = 0;
        private int second =0;
        private DateTime lastUpdate;
        private DayofWeek dayofWeek;
        private ReportSaveFrequency reportSaveFrequency;

        [XmlAttribute]
        public string ReportName { get => abbr_reportName; set => abbr_reportName = value; }
        [XmlAttribute]
        public string FullreportName { get => fullreportName; set => fullreportName = value; }
        [XmlAttribute]
        public bool IsEnableAutosaveReport { get => isEnableAutosaveReport; set => isEnableAutosaveReport = value; }
        [XmlAttribute]
        public int Hour { get => hour; set => hour = value; }
        [XmlAttribute]
        public int Minute { get => minute; set => minute = value; }
        [XmlAttribute]
        public int Second { get => second; set => second = value; }
        [XmlAttribute]
        public ReportSaveFrequency ReportSaveFrequency { get => reportSaveFrequency; set => reportSaveFrequency = value; }
        [XmlAttribute]
        public DayofWeek DayofWeek { get => dayofWeek; set => dayofWeek = value; }
        [XmlAttribute]
        public DateTime LastUpdate { get => lastUpdate; set => lastUpdate = value; }
    }
}

如果覆盖一个文件(或类似的流)并用较短的数据覆盖较长的文件而不截断它,就会发生这种情况。如果使用write(
FileMode.open
)打开一个随机访问的现有文件,它假定您要编辑内容,但不假定您只需要新的位。您看到的是旧负载中的额外字节,这些字节不会自动删除。有两种截断方法:

  • 打开文件时使用
    FileMode.Create
    ,它将文件初始化为零长度
  • 完成编写后,在
    流上使用
    SetLength
    ,告诉它新的长度-例如
    Stream.SetLength(Stream.Position)

  • 如果覆盖一个文件(或类似的流)并用较短的数据覆盖较长的文件而不截断它,就会发生这种情况。如果使用write(
    FileMode.open
    )打开一个随机访问的现有文件,它假定您要编辑内容,但不假定您只需要新的位。您看到的是旧负载中的额外字节,这些字节不会自动删除。有两种截断方法:

  • 打开文件时使用
    FileMode.Create
    ,它将文件初始化为零长度
  • 完成编写后,在
    流上使用
    SetLength
    ,告诉它新的长度-例如
    Stream.SetLength(Stream.Position)

  • 您的代码包含语法错误,因此我们无法真正信任您所声称的情况。请使用(FileStream fs=new FileStream(CONFIGFILEPATH),FileMode.Create)执行
    操作,而不是
    FileMode。打开
    确定,我将尝试。OpenOrCreate怎么样?以前尝试过打开或创建,但仍然相同。不,也不起作用,因为它被定义为指定操作系统应打开文件(如果存在);否则,应创建一个新文件。只有打开现有文件才是问题的根本原因。创建是唯一明智的选择,假设您可能遇到文件尚不存在的情况。您的代码包含语法错误,因此我们无法真正相信您所声称的情况。请使用(FileStream fs=new FileStream(CONFIGFILEPATH),FileMode.Create)执行
    ,而不是
    FileMode。打开
    好的,我会尝试一下。OpenOrCreate怎么样?以前尝试过打开或创建,但仍然相同。不,也不起作用,因为它被定义为指定操作系统应打开文件(如果存在);否则,应创建一个新文件。只有打开现有文件才是问题的根本原因。创建是唯一明智的选择,假设您可能遇到文件尚不存在的情况。执行选项2是否有特定的好处?@rene如果您控制创建/打开流的代码,则不存在执行选项2的特定好处?@rene如果您控制创建/打开流的代码,则不存在