814187 PRB: "System.INValidOperationException" error (from mkba) error (from mkba) error (from mkba) error (from mkba)

xiaoxiao2021-03-06  69

Beta Information This article discusses the beta version of some Microsoft product. The information in this article is provided as "as", if there is any change without notice.

Microsoft does not provide formal product support to this Beta product. For information on how to get support for Beta versions, see the documentation included in the beta product file, or view the web location you downloaded this version.

Symptom

XMLSerializer object Try serialization

When the NameValueCollection object, the following exception error message appears:

An Unhandled Exception of Type 'System.invalidOperationException' Occurred In System.xml.dll

Additional Information: You Must Implement The Add (System.String) Method on System.Collections.Specialized.NameValueCollection Because It inherits from iCault.

the reason

NameValueCollection is not directly realized

ICollection interface. in contrast,

NameValueCollection extension

NameObjectCollectionBase. This will be realized

Icollection interface, and

Replenishment in the NameValueCollection class

Add (system.string) method. In use

XMLSerializer

XMLSERIALIZER trial

NameValueCollection serialization or reverse sequence is general

Icollection. So it looks for default

Add (System.String). if there is not

ADD (System.String) method will exceed.

Alternative ways to resolve this issue, please use

The SOAPFormatter class is serialized instead of using XML for serialization.

Pay attention to use

SOAPFormatter class, you must add a reference to System.Runtime.Soap.dll.

Visual C # .NET code

The following code describes how to use in Visual C # .NET

SOAPFORMATTER:

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 ();

// address 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 how to use in Visual Basic .NET

SOAPFORMATTER:

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: /DataSoAppvb.xml", FileMode.Open, FileAccess.Read

DIM NAMVALCOLL1 AS NameValueCollection = sf1.deserialize (STRM2)

Strm2.close ()

'End of soap deserialization

End Sub

End module

This phenomenon is designed to make.

More information

Steps to reproduce the problem Start Microsoft Visual Studio .NET. On the File menu, point to "New" and click Project. Below "Project Types", click Visual C # items or "Visual Basic Project", and then click Console Application below "Template". Name the project MyConsoleApplication, then click OK. Replace existing code to: 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 ();

// address Some Sample Data to NameValueCollection Object

Namvalcoll.Add ("Name1", "Value1");

// serializing the namevalueCollection Object Using XMLSerializer

XMLSerializer myserializer = new xmlserializer (TypeF (NameValueCollection);

Stream strm1 = file.open (@ "c: /datasoap.xml", filemode.openorcreate, fileaccess.readwrite;

MySerializer.Serialize (strM1, namvalcoll);

Strm1.close ();

}

}

}

Use 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 XMLSerigIzer

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. At this point, the exception error message described in the "Symptom" section described herein will appear.

Refer to other information, click the article number below to see the article in the Microsoft Knowledge Base: 323503 Webcast: XML Serialization and Sample Code

314150 Info: Roadmap for XML Serialization in the .NET Framework

The information in this article applies to:

Microsoft Visual Basic .NET (2003) Microsoft Visual Basic .NET (2002) Microsoft Visual C # .NET (2003) Microsoft Visual C # .NET (2002) Microsoft XML Classes (included with the .NET Framework 1.0) Microsoft XML Classes (included with The .NET Framework 1.1)

Recent Updated: 2003-7-3 (1.4) Keyword Kberrmsg Kbxml KBSerial Kbcollections KbcollectionClass Kbprb KB814187 KBAUDDEVELOPER

转载请注明原文地址:https://www.9cbs.com/read-108967.html

New Post(0)