Microsoft KB Archive/884466

= You receive a &quot;System.IO.FileNotFoundException&quot; exception error message when you serialize an object to XML in a Visual Studio .NET or Visual Studio 2005 project =

Article ID: 884466

Article Last Modified on 11/14/2007

-

APPLIES TO


 * Microsoft Visual Studio 2005 Professional Edition
 * Microsoft Visual Studio 2005 Standard Edition
 * Microsoft Visual Studio .NET 2003 Enterprise Architect
 * Microsoft Visual Studio .NET 2003 Enterprise Developer
 * Microsoft Visual Studio .NET 2003 Professional Edition
 * Microsoft Visual Studio .NET 2003 Academic Edition
 * Microsoft Visual Studio .NET 2002 Enterprise Architect
 * Microsoft Visual Studio .NET 2002 Enterprise Developer
 * 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, or one of its dependencies, was not found.

Note  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.
 * 1) In the left pane, click General under Common Properties.
 * 2) In the right pane, change &quot;Serialization&quot; to &quot;Serialization1&quot; in the Root namespace box.



Steps to reproduce the behavior
 Start Visual Studio .NET or Visual Studio 2005. On the File menu, point to New, and then click Project.

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

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

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

By default, a button that is named Button1 is created.</li> In Solution Explorer, right-click Form1.vb, and then click View Code.</li>  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 </li>  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 </li>  Add the following code after the End Class line in the code: ' This is the class that will be serialized. Public Class OrderedItem <XmlElement(&quot;ItemName&quot;)> _ Public ItemName As String

<XmlElement(&quot;ItemDescription&quot;)> _ Public Description As String

<XmlElement(&quot;UnitPrice&quot;)> _ Public UnitPrice As Decimal

<XmlElement(&quot;Quantity&quot;)> _ Public Quantity As Integer

<XmlElement(&quot;LineTotal&quot;)> _ 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(&quot;simple.xml&quot;) End Sub

Private Sub SerializeObject(ByVal filename As String) Console.WriteLine(&quot;Writing With XmlTextWriter&quot;)

Dim serializer As New XmlSerializer(GetType(OrderedItem)) Dim i As New OrderedItem With i           .ItemName = &quot;Widget&quot; .Description = &quot;Regular Widget&quot; .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 </li> On the Build menu, click Build Solution.</li> On the Debug menu, click Run.

You receive the exception error message that is mentioned in the &quot;Symptoms&quot; section.</li></ol>

<div class="references_section">