XML 文档(0, 0)中有错误。缺少根元素。


 

function T Validate(string str){

using (MemoryStream ms = new MemoryStream())
{
foreach (byte _byte in Encoding.Default.GetBytes(str))
{
ms.WriteByte(_byte);
}

result = (T)new XmlSerializer(typeof(T)).Deserialize(ms);
ms.Close();
}

}


未处理 System.InvalidOperationException Message
="XML 文档(0, 0)中有错误。" Source="System.Xml" StackTrace: 在 System.Xml.Serialization.XmlSerializer.Deserialize(XmlReader xmlReader, String encodingStyle, XmlDeserializationEvents events) 在 System.Xml.Serialization.XmlSerializer.Deserialize(Stream stream) 在 NFLicense.Class1.Load[T](FileInfo file) 位置 E:\hy\WWW\abc\test.cs:行号 131 在 NFLicense.Class1.Load(String fileName) 位置 E:\hy\WWW\abc\test.cs:行号 209 在 NFLicense.Class1.Validate(String linesepath, String pubkey, String productname, String username) 位置 E:\hy\WWW\NFCRM\abc\test.cs:行号 217 在 NFCRM.Program.Main(String[] args) 位置 E:\hy\WWW\NFCRM\abc\Program.cs:行号 22 在 System.AppDomain._nExecuteAssembly(Assembly assembly, String[] args) 在 System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args) 在 Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly() 在 System.Threading.ThreadHelper.ThreadStart_Context(Object state) 在 System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state) 在 System.Threading.ThreadHelper.ThreadStart() InnerException: System.Xml.XmlException Message="缺少根元素。" Source="System.Xml" LineNumber=0 LinePosition=0 SourceUri="" StackTrace: 在 System.Xml.XmlTextReaderImpl.Throw(Exception e) 在 System.Xml.XmlTextReaderImpl.ThrowWithoutLineInfo(String res) 在 System.Xml.XmlTextReaderImpl.ParseDocumentContent() 在 System.Xml.XmlTextReaderImpl.Read() 在 System.Xml.XmlTextReader.Read() 在 System.Xml.XmlReader.MoveToContent() 在 Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderLicense.Read3_License() InnerException:

 

 

解决办法:

在序列化之前加:

ms.Position = 0;

function T Validate(string str){

using (MemoryStream ms = new MemoryStream())
{
foreach (byte _byte in Encoding.Default.GetBytes(str))
{
ms.WriteByte(_byte);
}

result = (T)new XmlSerializer(typeof(T)).Deserialize(ms);
ms.Close();
}

}

 

解决办法来源:http://www.cnblogs.com/nikytwo/archive/2009/03/27/1423032.html

posted @ 2013-04-19 17:59  HTL  阅读(11683)  评论(0编辑  收藏  举报
htl