我在反序列化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();
有什么想法吗?
答案 0 :(得分:19)
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);
}
答案 1 :(得分:0)
在运行时反序列化时指定root属性。
XmlRootAttribute xmlRoot = new XmlRootAttribute();
xmlRoot.ElementName = "entity";
xmlRoot.IsNullable = true;
XmlSerializer xs = new XmlSerializer(typeof(Object),xmlRoot);