Article ID: 814187
Article Last Modified on 9/24/2003
APPLIES TO
- Microsoft Visual Basic .NET 2003 Standard Edition
- Microsoft Visual Basic .NET 2002 Standard Edition
- Microsoft Visual C# .NET 2003 Standard Edition
- Microsoft Visual C# .NET 2002 Standard Edition
- Microsoft .NET Framework 1.0
- Microsoft .NET Framework 1.1
Beta Information
This article discusses a Beta release of a Microsoft product. The information in this article is provided as-is and is subject to change without notice.
No formal product support is available from Microsoft for this Beta product. For information about how to obtain support for a Beta release, see the documentation that is included with the Beta product files, or check the Web location from which you downloaded the release.
SYMPTOMS
When you try to serialize NameValueCollection objects by using the XMLSerializer object, you receive the following exception error message:
CAUSE
NameValueCollection does not directly implement the ICollection interface. Instead, NameValueCollection extends NameObjectCollectionBase. This implements the ICollection interface, and the overloaded Add(system.string) method is not implemented in the NameValueCollection class. When you use the XMLSerializer, the XmlSerializer tries to serialize or deserialize the NameValueCollection as a generic ICollection. Therefore, it looks for the default Add(System.String). In the absence of the Add(system.String) method, the exception is thrown.
WORKAROUND
To work around this problem, use the SoapFormatter class for serialization instead of using XML Serialization.
Note To use the SoapFormatter class, you must add a reference to System.Runtime.Serialization.Formatters.Soap.dll.
Visual C# .NET Code
The following code describes using the SOAPFormatter in Visual C# .NET:
using System; using System.IO; using System.Xml; using System.Xml.Serialization; using System.Collections.Specialized; using System.Runtime.Serialization.Formatters.Soap; namespace MyConsoleApplication { class Class1 { [STAThread] static void Main(string[] args) { // Defining a NameValueCollection object NameValueCollection namValColl = new NameValueCollection(); // Adding some sample data to NameValueCollection Object namValColl.Add("name1", "value1"); // Serializing NameValueCollection object by using SoapFormatter SoapFormatter sf = new SoapFormatter(); Stream strm1 = File.Open(@"C:\datasoap.xml", FileMode.OpenOrCreate,FileAccess.ReadWrite); sf.Serialize(strm1,namValColl); strm1.Close(); // End of SOAP Serialization // Deserializing the XML file into NameValueCollection object // by using SoapFormatter SoapFormatter sf1 = new SoapFormatter(); Stream strm2 = File.Open(@"C:\datasoap.xml", FileMode.Open,FileAccess.Read); NameValueCollection namValColl1 = (NameValueCollection)sf1.Deserialize(strm2); strm2.Close(); // End of SOAP Deserialization } } }
Visual Basic .NET Code
The following code describes using the SoapFormatter in Visual Basic .NET:
Imports System Imports System.IO Imports System.Xml.Serialization Imports System.Collections.Specialized Imports System.Runtime.Serialization.Formatters.Soap Module Module1 Sub Main() ' Defining a NameValueCollection object Dim namValColl As New NameValueCollection() ' Adding some sample data to NameValueCollection Object namValColl.Add("name1", "value1") ' Serializing NameValueCollection object by using SoapFormatter Dim sf As SoapFormatter = New SoapFormatter() Dim strm1 As Stream = File.Open("C:\datasoapvb.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite) sf.Serialize(strm1, namValColl) strm1.Close() ' End of SOAP Serialization ' Deserializing the XML file into NameValueCollection object ' by using SoapFormatter Dim sf1 As SoapFormatter = New SoapFormatter() Dim strm2 As Stream = File.Open("C:\datasoapvb.xml", FileMode.Open, FileAccess.Read) Dim namValColl1 As NameValueCollection = sf1.Deserialize(strm2) strm2.Close() ' End of SOAP Deserialization End Sub End Module
STATUS
This behavior is by design.
MORE INFORMATION
Steps to Reproduce the Behavior
- Start Microsoft Visual Studio .NET.
- On the File menu, point to New, and then click Project.
- Under Project Types, click Visual C# Projects or Visual Basic Projects, and then click Console Application under Templates.
- Name the project MyConsoleApplication, and then click OK.
Replace the existing code with the following code:
Visual C# .NET Code
using System; using System.IO; using System.Xml.Serialization ; using System.Collections.Specialized; namespace MyConsoleApplication { class Class1 { [STAThread] static void Main(string[] args) { // Defining a NameValueCollection object. NameValueCollection namValColl = new NameValueCollection(); // Adding some sample data to NameValueCollection Object namValColl.Add("name1", "value1"); // Serializing the NameValueCollection object using XMLSerializer XmlSerializer mySerializer = new XmlSerializer(typeof(NameValueCollection)); Stream strm1 = File.Open(@"C:\datasoap.xml", FileMode.OpenOrCreate,FileAccess.ReadWrite); mySerializer.Serialize(strm1, namValColl); strm1.Close(); } } }
Using Visual Basic .NET Code
Imports System Imports System.IO Imports System.Xml.Serialization Imports System.Collections.Specialized Module Module1 Sub Main() ' Defining a NameValueCollection object. Dim namValColl As New NameValueCollection ' Adding some sample data to NameValueCollection Object namValColl.Add("name1", "value1") ' Serializing the NameValueCollection object using XMLSerializer Dim mySerializer As New XmlSerializer(GetType(NameValueCollection)) Dim strm1 As Stream strm1 = File.Open("C:\datasoap.xml", FileMode.OpenOrCreate, FileAccess.ReadWrite) mySerializer.Serialize(strm1, namValColl) strm1.Close() End Sub End Module
- On the Debug menu, click Start. You receive the exception error message that is described in the "Symptoms" section of this article.
REFERENCES
For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:
323503 WebCast: XML Serialization and Sample Code
314150 INFO: Roadmap for XML Serialization in the .NET Framework
Keywords: kberrmsg kbprb kbserial kbcollections kbcollectionclass KB814187