为什么我收到InvalidOperationException?

时间:2014-01-20 19:15:42

标签: c# winforms

我有这段代码:

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: 

1 个答案:

答案 0 :(得分:3)

  

无法找到文件'D:\ C-Sharp \ Download   文件\下载 - 文件 - 项目 - 版本-012 \下载   文件\ BIN \ 86 \推出\ configData.xml'

检查文件是否确实存在。如果它包含在解决方案中,请检查是否将内容设置为编译类型,将复制到输出文件夹设置为“如果更新”。