根出现XML反序列化错误

本文关键字:反序列化 错误 XML | 更新日期: 2023-09-27 18:21:58

我在反序列化XML:时遇到问题

System.InvalidOperationException was unhandled
  Message=There is an error in XML document (0, 0).
  Source=System.Xml
  StackTrace:
       at System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events)
       at System.Xml.Serialization.XmlSerializer.Deserialize(TextReader textReader)
       at xsdToObject.Program.Main(String[] args) in D:'Old Documents'Projects'xsdToObject'xsdToObject'Program.cs:line 20
       at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
       at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
       at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
       at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart()
  InnerException: System.Xml.XmlException
       Message=Root element is missing.
       Source=System.Xml
       LineNumber=0
       LinePosition=0
       SourceUri=""
       StackTrace:
            at System.Xml.XmlTextReaderImpl.Throw(Exception e)
            at System.Xml.XmlTextReaderImpl.ParseDocumentContent()
            at System.Xml.XmlTextReaderImpl.Read()
            at System.Xml.XmlTextReader.Read()
            at System.Xml.XmlReader.MoveToContent()
            at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderentityColumnsColumnArray.Read3_ArrayOfEntityColumnsColumn()
       InnerException: 

当进入内部异常详细信息时,主要错误消息如下:{"根元素丢失。"}

我的xml文件如下:

<?xml version="1.0" encoding="utf-8" ?>
<entity name="ScheduleTaskEntity" schema="bclscheduler" namespace="http://www.bcl-gaming.com/data/1">
<columns>
<column name="Id" type="identity" />
<column name="UID" type="UID" />
<column name="Timestamp" type="timestamp"/>
<column name="DateCreated" type="datecreated"/>
<column name="Enabled" type="bool" />
<column name="TypeFullname" type="string" size="500" />
<column name="PropertiesLOB" type="propertybag" />
<column name="DisplayName" type="string" size="64" />
<column name="LastDateExecuted" type="datetime" nullable="true" />
<column name="LastMessage" type="string" />
<column name="LastSessionUID" type="guid" nullable="true" />
<column name="CurrentState" type="string" />
<column name="LastDateStarted" type="datetime" nullable="true"/>
<column name="SingleInstance" type="boolean" />
<column name="ExecuteCount" type="bigint" />
<column name="ErrorCount" type="bigint" />      
</columns>
</entity>

我的代码如下:

entity e;
System.Xml.Serialization.XmlSerializer x = new System.Xml.Serialization.XmlSerializer(typeof(entityColumnsColumn[]));
StreamReader reader = new StreamReader(@"D:'Old Documents'Projects'xsdToObject'xsdToObject'ScheduleTaskEntityTest.xml");
reader.ReadToEnd();
//it gives me the error here
e = (entity)x.Deserialize(reader);
reader.Close();

有什么想法吗?

根出现XML反序列化错误

reader.ReadToEnd();
//it gives me the error here
e = (entity)x.Deserialize(reader);

问题是reader.ReadToEnd()——在这之后就没有什么可读的了,只有在你试图反序列化的时候——这一定会失败。相反,只需直接使用StreamReader:

using(StreamReader reader = new StreamReader(@"D:'Old Documents'Projects'xsdToObject'xsdToObject'ScheduleTaskEntityTest.xml"))
{
    e = (entity)x.Deserialize(reader);
}

在运行时反序列化时指定根属性。

XmlRootAttribute xmlRoot = new XmlRootAttribute();
xmlRoot.ElementName = "entity";    
xmlRoot.IsNullable = true;
XmlSerializer xs = new XmlSerializer(typeof(Object),xmlRoot);