共用方式為


XmlSerializer.Serialize Method (XmlWriter, Object, XmlSerializerNamespaces)

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Serializes the specified Object and writes the XML document to a file using the specified XmlWriter and references the specified namespaces.

Namespace:  System.Xml.Serialization
Assembly:  System.Xml.Serialization (in System.Xml.Serialization.dll)

Syntax

'Declaration
Public Sub Serialize ( _
    xmlWriter As XmlWriter, _
    o As Object, _
    namespaces As XmlSerializerNamespaces _
)
public void Serialize(
    XmlWriter xmlWriter,
    Object o,
    XmlSerializerNamespaces namespaces
)

Parameters

Exceptions

Exception Condition
InvalidOperationException

An error occurred during serialization. The original exception is available using the InnerException property.

Remarks

When the Serialize method is invoked, the public fields and read/write properties of an object are converted into XML. Methods, indexers, private fields, and read-only properties are not serialized. To serialize all fields and properties, both public and private, use the BinaryFormatter.

Use the xmlWriter parameter to specify an object that derives from the abstract XmlWriter class, which is designed to write XML documents. The XmlTextWriter derives from the XmlWriter.

NoteNote:

The XmlSerializer cannot serialize arrays of ArrayList and arrays of List<T>.

Platform Notes

Silverlight for Windows Phone Silverlight for Windows Phone

 The XmlSerializer.Serialize method throws an InvalidOperationException if the XmlSerializer object is initialized with a type parameter that contains an array of objects of type XElement.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.