zoukankan      html  css  js  c++  java
  • 记录一次错误处理 (xml序列化和反序列化相关)

    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:DefctFrameworkcodesubmitDSDetectSystemDetectSys.BLLConfig.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="Glaqingdao.gla" />
        <Setter Key="GlaFile2" Value="Glashiyan2.gla" />
        <Setter Key="region1" Value="tmpLayer1片区.lgd;tmpLayerpianqu.lgd" />
        <Setter Key="region2" Value="tmpLayer2pianqu.lgd" />
        <Setter Key="region3" Value="tmpLayer3pianqu.lgd" />
        <Setter Key="region4" Value="tmpLayer4pianqu.lgd" />
        <Setter Key="region5" Value="tmpLayer5pianqu.lgd" />
        <Setter Key="region6" Value="tmpLayer6pianqu.lgd" />
        <Setter Key="region7" Value="tmpLayer7pianqu.lgd" />
        <Setter Key="region8" Value="tmpLayer8pianqu.lgd" />
        <Setter Key="region9" Value="tmpLayer9pianqu.lgd" />
        <Setter Key="region10" Value="tmpLayer10pianqu.lgd" />
        <Setter Key="region11" Value="tmpLayer11pianqu.lgd" />
        <Setter Key="region12" Value="tmpLayer12pianqu.lgd" />
        <Setter Key="LastSelectedDir" Value="E:DefctFramework数据cout8" />
      </Settings>
      <Catalogs>
        <Catalog Key="PATH_IMAGE_ICON_LABEL" Value="ResourceimagesIconLabel" />
        <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="resourceimages" />
        <Catalog Key="PATH_FRAME" Value="resourceframe" />
        <Catalog Key="PATH_THREEDLL" Value="ThreeDll" />
        <Catalog Key="PATH_JingJieQu" Value="tmpLayer警戒区.lgd" />
        <Catalog Key="PATH_IMAGE" Value="resourceimages" />
        <Catalog Key="PATH_FRAME" Value="resourceframe" />
        <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

  • 相关阅读:
    centos7 yum错误相关
    centos7 jenkins
    vim 常用命令
    Effective STL(第7条)
    【hihoCoder】1049.后序遍历
    C++ 单元测试 Cpputest
    【hihoCoder】1041. 国庆出游
    LeetCode(43. Multiply Strings)
    【LeetCode】16. 4Sum
    【LeetCode】1. Two Sum
  • 原文地址:https://www.cnblogs.com/xinyf/p/10407180.html
Copyright © 2011-2022 走看看