DataContractSerializer.WriteObjectContent Method

Definition

Writes the XML content without the wrapping element using the specified writer.

Overloads

WriteObjectContent(XmlDictionaryWriter, Object)

Writes the XML content using an XmlDictionaryWriter.

WriteObjectContent(XmlWriter, Object)

Writes the XML content using an XmlWriter.

WriteObjectContent(XmlDictionaryWriter, Object)

Writes the XML content using an XmlDictionaryWriter.

public:
 override void WriteObjectContent(System::Xml::XmlDictionaryWriter ^ writer, System::Object ^ graph);
public override void WriteObjectContent (System.Xml.XmlDictionaryWriter writer, object graph);
public override void WriteObjectContent (System.Xml.XmlDictionaryWriter writer, object? graph);
override this.WriteObjectContent : System.Xml.XmlDictionaryWriter * obj -> unit
Public Overrides Sub WriteObjectContent (writer As XmlDictionaryWriter, graph As Object)

Parameters

writer
XmlDictionaryWriter

The XmlDictionaryWriter used to write the stream.

graph
Object

The object to write to the stream.

Exceptions

The type being serialized does not conform to data contract rules. For example, the DataContractAttribute attribute has not been applied to the type.

There is a problem with the instance being written.

Examples

The following example creates an object to serialize, an instance of the DataContractSerializer, and an instance of the XmlDictionaryWriter class. The example uses the WriteStartObject, WriteObjectContent, and WriteObjectContent methods to write the object data into the XML document.

public static void WriteObjectContentInDocument(string path)
{
    // Create the object to serialize.
    Person p = new Person("Lynn", "Tsoflias", 9876);

    // Create the writer object.
    FileStream fs = new FileStream(path, FileMode.Create);
    XmlDictionaryWriter writer =
        XmlDictionaryWriter.CreateTextWriter(fs);

    DataContractSerializer ser =
        new DataContractSerializer(typeof(Person));

    // Use the writer to start a document.
    writer.WriteStartDocument(true);

    // Use the writer to write the root element.
    writer.WriteStartElement("Company");

    // Use the writer to write an element.
    writer.WriteElementString("Name", "Microsoft");

    // Use the serializer to write the start,
    // content, and end data.
    ser.WriteStartObject(writer, p);
    ser.WriteObjectContent(writer, p);
    ser.WriteEndObject(writer);

    // Use the writer to write the end element and
    // the end of the document.
    writer.WriteEndElement();
    writer.WriteEndDocument();

    // Close and release the writer resources.
    writer.Flush();
    fs.Flush();
    fs.Close();
}
Public Shared Sub WriteObjectContentInDocument(ByVal path As String) 
    ' Create the object to serialize.
    Dim p As New Person("Lynn", "Tsoflias", 9876)
    
    ' Create the writer.
    Dim fs As New FileStream(path, FileMode.Create)
    Dim writer As XmlDictionaryWriter = XmlDictionaryWriter.CreateTextWriter(fs)
    
    Dim ser As New DataContractSerializer(GetType(Person))
    
    ' Use the writer to start a document.
    writer.WriteStartDocument(True)
    ' Use the writer to write the root element.
    writer.WriteStartElement("Company")
    ' Use the writer to write an element.
    writer.WriteElementString("Name", "Microsoft")

    ' Use the serializer to write the start,
    ' content, and end data.
    ser.WriteStartObject(writer, p)
    ser.WriteObjectContent(writer, p)
    ser.WriteEndObject(writer)
    
    ' Use the writer to write the end element and
    ' the end of the document.
    writer.WriteEndElement()
    writer.WriteEndDocument()
    
    ' Close and release the writer resources.
    writer.Flush()
    fs.Flush()
    fs.Close()

End Sub

Applies to

WriteObjectContent(XmlWriter, Object)

Writes the XML content using an XmlWriter.

public:
 override void WriteObjectContent(System::Xml::XmlWriter ^ writer, System::Object ^ graph);
public override void WriteObjectContent (System.Xml.XmlWriter writer, object graph);
public override void WriteObjectContent (System.Xml.XmlWriter writer, object? graph);
override this.WriteObjectContent : System.Xml.XmlWriter * obj -> unit
Public Overrides Sub WriteObjectContent (writer As XmlWriter, graph As Object)

Parameters

writer
XmlWriter

The XmlWriter used to write the stream.

graph
Object

The object to write to the stream.

Exceptions

The type being serialized does not conform to data contract rules. For example, the DataContractAttribute attribute has not been applied to the type.

There is a problem with the instance being written.

Applies to