You receive a "System.IO.FileNotFoundException" exception error message when you serialize an object to XML in a Visual Studio .NET or Visual Studio 2005 project (884466)



The information in this article applies to:

  • Microsoft Visual Studio 2005 Professional Edition
  • Microsoft Visual Studio 2005 Standard Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2003), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2003), Professional Edition
  • Microsoft Visual Studio .NET (2003), Academic Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Architect Edition
  • Microsoft Visual Studio .NET (2002), Enterprise Developer Edition
  • Microsoft Visual Studio .NET (2002), Professional Edition
  • Microsoft Visual Studio .NET (2002), Academic Edition

SYMPTOMS

You receive the following exception error message when you try to serialize an object in a Microsoft Visual Studio .NET or Microsoft Visual Studio 2005 application:
An unhandled exception of type 'System.IO.FileNotFoundException' occurred in mscorlib.dll Additional information: File or assembly name <AssemblyName>, or one of its dependencies, was not found.
Note AssemblyName is a placeholder for the assembly name that the computer generates at run time.

You may receive the same exception error message when you try to deserialize a serialized object in an application that has the Serialization namespace as the root namespace.

CAUSE

When you serialize custom classes, the Microsoft .NET Framework makes a private compilation of some code. Therefore, this problem occurs when the root namespace of your project is Serialization and your project includes the System.XML.Serialization namespace.

For example, if your custom class is named OrderedItem, the qualified name of the OrderedItem class is Serialization.OrderedItem. This name conflicts with the name of the System.Xml.Serialization namespace. Because the two names conflict, the compiler cannot find the OrderedItem class.

WORKAROUND

To work around this problem, change the RootNamespace property in the project properties. To do this, follow these steps:

Note Before you change the root namespace, make sure that all the files and forms of that project are closed.
  1. In Solution Explorer, right-click Serialization.
  2. Click Properties.

    The Serialization Property Pages dialog box appears.
  3. In the left pane, click General under Common Properties.
  4. In the right pane, change "Serialization" to "Serialization1" in the Root namespace box.

MORE INFORMATION

Steps to reproduce the behavior

  1. Start Visual Studio .NET or Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.

    The New Project dialog box appears.
  3. Under Project Types, click Visual Basic Projects.

    Note In Visual Studio 2005, click Visual Basic.
  4. Under Templates, click Windows Application.
  5. Type Serialization in the Name box, and then click OK.

    By default, a form that is named Form1 is created.
  6. Add a Button control to the form.

    By default, a button that is named Button1 is created.
  7. In Solution Explorer, right-click Form1.vb, and then click View Code.
  8. Add the following code at the beginning of the Form1.vb file:
    Imports System
    Imports System.IO
    Imports System.Text
    Imports System.Xml
    Imports System.Xml.Serialization
  9. Add the following code before the End Class line in the code:
        Private Sub Button1_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles Button1.Click
            Dim obj As New Test
            obj.Main()
        End Sub
  10. Add the following code after the End Class line in the code:
    ' This is the class that will be serialized.
    Public Class OrderedItem
        <XmlElement("ItemName")> _
        Public ItemName As String
    
        <XmlElement("ItemDescription")> _
        Public Description As String
    
        <XmlElement("UnitPrice")> _
        Public UnitPrice As Decimal
    
        <XmlElement("Quantity")> _
        Public Quantity As Integer
    
        <XmlElement("LineTotal")> _
        Public LineTotal As Decimal
    
        'A custom method used to calculate price per item.
        Public Sub Calculate()
            LineTotal = UnitPrice * Quantity
        End Sub
    End Class
    
    Public Class Test
    
        Public Shared Sub Main()
            Dim t As New Test
            ' Write a purchase order.
            t.SerializeObject("simple.xml")
        End Sub
    
        Private Sub SerializeObject(ByVal filename As String)
            Console.WriteLine("Writing With XmlTextWriter")
    
            Dim serializer As New XmlSerializer(GetType(OrderedItem))
            Dim i As New OrderedItem
            With i
                .ItemName = "Widget"
                .Description = "Regular Widget"
                .Quantity = 10
                .UnitPrice = CDec(2.3)
                .Calculate()
            End With
            ' Create an object of class XmlTextWriter by using an object of class FileStream.
            Dim fs As New FileStream(filename, FileMode.Create)
            Dim writer As New XmlTextWriter(fs, New UTF8Encoding)
            ' Serialize by using the object of class XmlTextWriter.
            serializer.Serialize(writer, i)
            writer.Close()
        End Sub
    End Class
  11. On the Build menu, click Build Solution.
  12. On the Debug menu, click Run.

    You receive the exception error message that is mentioned in the "Symptoms" section.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web site: For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:

314150 Roadmap for XML serialization in the .NET Framework

815813 Serialize an object to XML by using Visual C# .NET

316730 Serialize and deserialize XML in Visual Basic .NET

823196 You receive a "System.IO.FileNotFoundException" error when the client application calls a Web service


Modification Type:MajorLast Reviewed:2/28/2006
Keywords:kbvs2005swept kbvs2005applies kbXML kbSerial kberrmsg kbtshoot kbprb KB884466 kbAudDeveloper