记录一次错误处理 (xml序列化和反序列化相关)

时间:2021-03-09 07:22:56

XML序列化后,反序列化时出现错误

报错现象

System.InvalidOperationException: XML 文档(40, 11)中有错误。 ---> System.Xml.XmlException: 根级别上的数据无效。 第 40 行,位置 11。
在 System.Xml.XmlTextReaderImpl.Throw(Exception e)
在 System.Xml.XmlTextReaderImpl.ParseRootLevelWhitespace()
在 System.Xml.XmlTextReaderImpl.ParseDocumentContent()
在 System.Xml.XmlReader.ReadEndElement()
在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderConfigs.Read3_Configs(Boolean isNullable, Boolean checkType)
在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderConfigs.Read4_Configs()
--- 内部异常堆栈跟踪的结尾 ---
在 System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
在 System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream)
在 DetectSys.Configs.LoadConfig() 位置 E:\DefctFramework\code\submitDS\DetectSystem\DetectSys.BLL\Config.cs:行号 55

报错时,xml文件

查看了一下序列化的文件,在文件最后总是会多出一些垃圾字符导致反序列化错误
<?xml version="1.0"?>
<Configs xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Settings>
<Setter Key="AppTitle" Value="三维可视化" />
<Setter Key="WeatherCityName" Value="达州" />
<Setter Key="UavFlyCenterPoint" Value="107.204756400529,31.1305284689069,160.0" />
<Setter Key="SqlDbString" Value="Server=127.0.0.1;uid=root;Password=root;Database=d3videofusion" />
<Setter Key="IGetWeatherImpl" Value="WeatherPlugins.Impl.LocalWheather,WeatherPlugins.dll" />
<Setter Key="autoBallonLayers" Value="tmpLayer\建筑信息.lgd;tmpLayer\建筑信息2.lgd" />
<Setter Key="GlaFile" Value="Gla\qingdao.gla" />
<Setter Key="GlaFile2" Value="Gla\shiyan2.gla" />
<Setter Key="region1" Value="tmpLayer\1片区.lgd;tmpLayer\pianqu.lgd" />
<Setter Key="region2" Value="tmpLayer\2pianqu.lgd" />
<Setter Key="region3" Value="tmpLayer\3pianqu.lgd" />
<Setter Key="region4" Value="tmpLayer\4pianqu.lgd" />
<Setter Key="region5" Value="tmpLayer\5pianqu.lgd" />
<Setter Key="region6" Value="tmpLayer\6pianqu.lgd" />
<Setter Key="region7" Value="tmpLayer\7pianqu.lgd" />
<Setter Key="region8" Value="tmpLayer\8pianqu.lgd" />
<Setter Key="region9" Value="tmpLayer\9pianqu.lgd" />
<Setter Key="region10" Value="tmpLayer\10pianqu.lgd" />
<Setter Key="region11" Value="tmpLayer\11pianqu.lgd" />
<Setter Key="region12" Value="tmpLayer\12pianqu.lgd" />
<Setter Key="LastSelectedDir" Value="E:\DefctFramework\数据\cout8" />
</Settings>
<Catalogs>
<Catalog Key="PATH_IMAGE_ICON_LABEL" Value="Resource\images\IconLabel" />
<Catalog Key="PATH_FengKongQu" Value="tmpLayer\封控区.lgd" />
<Catalog Key="PATH_HeXinQu" Value="tmpLayer\核心区.lgd" />
<Catalog Key="PATH_JingJieQu" Value="tmpLayer\警戒区.lgd" />
<Catalog Key="PATH_IMAGE" Value="resource\images" />
<Catalog Key="PATH_FRAME" Value="resource\frame" />
<Catalog Key="PATH_THREEDLL" Value="ThreeDll" />
<Catalog Key="PATH_JingJieQu" Value="tmpLayer\警戒区.lgd" />
<Catalog Key="PATH_IMAGE" Value="resource\images" />
<Catalog Key="PATH_FRAME" Value="resource\frame" />
<Catalog Key="PATH_THREEDLL" Value="ThreeDll" />
<Catalog Key="key" Value="value" />
</Catalogs>
</Configs>Configs>

报错的直接原因是xml文件后面多了

 Configs>
这种现象我称之为 拖尾现象
注意,多出的垃圾字符是不是随意的,是有规律的。

分析问题

但是为什么会多呢

先检查序列化代码。代码如下

public void Save()
{
if (config != null)
{
if (!File.Exists(saveFile))
{
File.Create(saveFile).Close();
}
XmlSerializer serializer = new XmlSerializer(typeof(Configs));
using (FileStream stream = new FileStream(saveFile, FileMode.Open))
{
serializer.Serialize(stream, config);
}
}
}

经过分析,问题就出在文件的打开方式上。

FileMode.Open方式,会覆盖原始文件。如果新的对象长度小于原始对象长度,就会出现拖尾现象。

解决问题

把FileStream打开方式改为Truncate,问题解决

public void Save()
{
if (config != null)
{
if (!File.Exists(saveFile))
{
File.Create(saveFile).Close();
}
XmlSerializer serializer = new XmlSerializer(typeof(Configs));
using (FileStream stream = new FileStream(saveFile, FileMode.Truncate))
{
serializer.Serialize(stream, config);
}
}
}

tag