user3200169
user3200169

Reputation: 275

Why am I getting InvalidOperationException?

I have this code:

private string mConfigFileName = "configData.xml";

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

I'm using a breakpoint on the line mConfiguration = (Configuration)XmlUtility.Deserialize(mConfiguration.GetType(), mConfigFileName); and it's going to this method:

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;
        }      

Then on the line Object data = xs.Deserialize(xmlReader); it's jumping back to the catch of the Getconfiguration method:

catch 
            {
                mConfiguration = new Configuration();
            }

The exception:

There is an error in XML document (0, 0)

This is the xml document content:

<?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>

This is the full exception message:

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: 

EDIT:

I added the xml file as file to the resources of my project. And changed to code to:

private string mConfigFileName = Properties.Resources.configData;

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

Now it's jumping to the catch again with a different exception:

Illegal characters in path

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: 

Upvotes: 1

Views: 1284

Answers (1)

PashaPash
PashaPash

Reputation: 2066

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

Check that file actually exists. If it included in solution, check that is has Content set as compile type and Copy to Output folder set to "If Newer".

Upvotes: 3

Related Questions