2014-01-20 116 views
1

我有这样的代码:为什么我得到InvalidOperationException?

private string mConfigFileName = "configData.xml"; 

     private void GetConfiguration() 
     { 
      try 
      {     
       mConfiguration = (Configuration)XmlUtility.Deserialize(mConfiguration.GetType(), mConfigFileName);     
      } 
      catch 
      { 
       mConfiguration = new Configuration(); 
      } 
     } 

我使用就行了mConfiguration = (Configuration)XmlUtility.Deserialize(mConfiguration.GetType(), mConfigFileName);一个断点,这将这个方法:

public static Object Deserialize(Type type, string fileName) 
     { 
      XmlSerializer xs = new XmlSerializer(type); 

      XmlTextReader xmlReader = new XmlTextReader(fileName); 
      Object data = xs.Deserialize(xmlReader); 

      xmlReader.Close(); 

      return data; 
     }  

然后上线Object data = xs.Deserialize(xmlReader);它跳回的抓该方法Getconfiguration:

catch 
      { 
       mConfiguration = new Configuration(); 
      } 

例外:

有XML文档中的错误(0,0)

这是XML文档内容:

<?xml version="1.0" encoding="utf-8"?> 
<Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> 
    <LocationX>877</LocationX> 
    <LocationY>498</LocationY> 
    <CloseOnMouseUp>true</CloseOnMouseUp> 
    <DoubleBuffered>true</DoubleBuffered> 
    <HideMouseCursor>true</HideMouseCursor> 
    <RememberLastPoint>true</RememberLastPoint> 
    <ReturnToOrigin>true</ReturnToOrigin> 
    <ShowInTaskbar>false</ShowInTaskbar> 
    <TopMostWindow>true</TopMostWindow> 
    <MagnifierWidth>150</MagnifierWidth> 
    <MagnifierHeight>150</MagnifierHeight> 
    <ZoomFactor>3</ZoomFactor> 
    <SpeedFactor>0.35</SpeedFactor> 
</Configuration> 

这是完整的异常消息:

System.InvalidOperationException was caught 
    HResult=-2146233079 
    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(XmlReader xmlReader) 
     at mws.XmlUtility.Deserialize(Type type, String fileName) in d:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\XmlUtility.cs:line 53 
     at mws.MagnifierMainForm.GetConfiguration() in d:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\MagnifierMainForm.cs:line 110 
    InnerException: System.IO.FileNotFoundException 
     HResult=-2147024894 
     Message=Could not find file 'D:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\bin\x86\Release\configData.xml'. 
     Source=mscorlib 
     FileName=D:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\bin\x86\Release\configData.xml 
     StackTrace: 
      at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 
      at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) 
      at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize) 
      at System.Xml.XmlDownloadManager.GetStream(Uri uri, ICredentials credentials, IWebProxy proxy, RequestCachePolicy cachePolicy) 
      at System.Xml.XmlUrlResolver.GetEntity(Uri absoluteUri, String role, Type ofObjectToReturn) 
      at System.Xml.XmlTextReaderImpl.OpenUrlDelegate(Object xmlResolver) 
      at System.Threading.CompressedStack.runTryCode(Object userData) 
      at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData) 
      at System.Threading.CompressedStack.Run(CompressedStack compressedStack, ContextCallback callback, Object state) 
      at System.Xml.XmlTextReaderImpl.OpenUrl() 
      at System.Xml.XmlTextReaderImpl.Read() 
      at System.Xml.XmlTextReader.Read() 
      at System.Xml.XmlReader.MoveToContent() 
      at Microsoft.Xml.Serialization.GeneratedAssembly.XmlSerializationReaderConfiguration.Read3_Configuration() 
     InnerException: 

编辑:

我将xml文件作为文件添加到我的项目资源中。 而改为代码:

private string mConfigFileName = Properties.Resources.configData; 

     private void GetConfiguration() 
     { 
      try 
      {     
       mConfiguration = (Configuration)XmlUtility.Deserialize(mConfiguration.GetType(), mConfigFileName);     
      } 
      catch 
      { 
       mConfiguration = new Configuration(); 
      } 
     } 

现在它有一个不同的异常再次跳到陷阱:

非法字符的路径

System.ArgumentException was caught 
    HResult=-2147024809 
    Message=Illegal characters in path. 
    Source=mscorlib 
    StackTrace: 
     at System.IO.Path.CheckInvalidPathChars(String path, Boolean checkAdditional) 
     at System.IO.Path.NormalizePath(String path, Boolean fullCheck, Int32 maxPathLength, Boolean expandShortPaths) 
     at System.IO.Path.GetFullPathInternal(String path) 
     at System.IO.Path.GetFullPath(String path) 
     at System.Xml.XmlResolver.ResolveUri(Uri baseUri, String relativeUri) 
     at System.Xml.XmlUrlResolver.ResolveUri(Uri baseUri, String relativeUri) 
     at System.Xml.XmlTextReaderImpl..ctor(String url, XmlNameTable nt) 
     at System.Xml.XmlTextReader..ctor(String url) 
     at mws.XmlUtility.Deserialize(Type type, String fileName) in d:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\XmlUtility.cs:line 52 
     at mws.MagnifierMainForm.GetConfiguration() in d:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\MagnifierMainForm.cs:line 110 
    InnerException: 
+2

查看内部异常:'InnerException:System.IO.FileNotFoundException'。它无法找到'D:\ C-Sharp \下载文件\正在下载-File-Project-Version-012 \正在下载File \ bin \ x86 \ Release \ configData.xml'。 – Tim

+0

编辑我的问题。我可以将文件复制到我当前的项目调试或发布目录中,但是如果我想在其他项目上使用它,或者将它发送给某人,我将需要随时复制它。 – user3200169

回答

3

Could not find file 'D:\C-Sharp\Download File\Downloading-File-Project-Version-012\Downloading File\bin\x86\Release\configData.xml'

检查文件是否确实存在。如果它包含在解决方案中,请检查是否将Content设置为编译类型,并将Copy to Output文件夹设置为“If Newer”。

+0

PashaPash我做了,我试图将文件添加到我的项目资源。但现在我得到另一个例外。我想只是添加文件作为资源不好。它将变量mConfigFileName中的字符串放入xml的内容中,而不是其文件名。私人字符串mConfigFileName = Properties.Resources.configData;在这一行我看到文件的内容。那么如何将文件添加到我的项目中并将其名称用作文件名而不是其内容? – user3200169

+0

我刚刚试图将它设置为编译,也如果更新,但然后我得到错误:错误命名空间不能直接包含成员,如字段或方法\t D:\ C-Sharp \下载文件\下载文件-Project-Version-012 \正在下载File \ Resources \ configData.xml 正在下载文件 – user3200169

+0

错误发生在第一个char上的第一行的xml内容“<”符号小于 – user3200169