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:
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.
- In Solution Explorer, right-click Serialization.
- Click Properties.
The Serialization Property Pages dialog box appears.
- In the left pane, click General under Common Properties.
- In the right pane, change "Serialization" to "Serialization1" in the Root namespace box.
MORE INFORMATION
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. - In Solution Explorer, right-click Form1.vb, and then click View Code.
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
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
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
- On the Build menu, click Build Solution.
- 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
Keywords: kbvs2005swept kbvs2005applies kbxml kbserial kberrmsg kbtshoot kbprb KB884466