NetDataContractSerializer.WriteEndObject Metod
Definition
Viktigt
En del information gäller för förhandsversionen av en produkt och kan komma att ändras avsevärt innan produkten blir allmänt tillgänglig. Microsoft lämnar inga garantier, uttryckliga eller underförstådda, avseende informationen som visas här.
Skriver det avslutande XML-elementet med den angivna skrivaren.
Överlagringar
| Name | Description |
|---|---|
| WriteEndObject(XmlDictionaryWriter) |
Skriver det avslutande XML-elementet med hjälp av en XmlDictionaryWriter. |
| WriteEndObject(XmlWriter) |
Skriver det avslutande XML-elementet med hjälp av en XmlWriter. |
Kommentarer
Metoderna WriteStartObject, WriteObjectContentoch WriteEndObject används i följd för att skriva den fullständiga serialiseringen med hjälp av mönstret: skrivstart, skrivinnehåll och skrivslut. De tre metoderna anropas också av WriteObject metoden.
WriteEndObject(XmlDictionaryWriter)
Skriver det avslutande XML-elementet med hjälp av en XmlDictionaryWriter.
public:
override void WriteEndObject(System::Xml::XmlDictionaryWriter ^ writer);
public override void WriteEndObject(System.Xml.XmlDictionaryWriter writer);
override this.WriteEndObject : System.Xml.XmlDictionaryWriter -> unit
Public Overrides Sub WriteEndObject (writer As XmlDictionaryWriter)
Parametrar
- writer
- XmlDictionaryWriter
Används XmlDictionaryWriter för att skriva XML-dokumentet eller dataströmmen.
Undantag
är inställt på writernull.
Exempel
I följande exempel skapas ett objekt som ska serialiseras, en instans av NetDataContractSerializeroch en instans av XmlDictionaryWriter klassen. I exemplet används WriteStartObjectmetoderna , WriteObjectContentoch WriteEndObject för att skriva objektdata till XML-dokumentet.
public sealed class ShowWriteStartObject
{
public static void WriteObjectData(string path)
{
// Create the object to serialize.
Person p = new Person("Lynn", "Tsoflias", 9876);
// Create the writer.
FileStream fs = new FileStream(path, FileMode.Create);
XmlDictionaryWriter writer =
XmlDictionaryWriter.CreateTextWriter(fs);
NetDataContractSerializer ser =
new NetDataContractSerializer();
// Use the writer to start a document.
writer.WriteStartDocument(true);
// Use the serializer to write the start of the
// object data. Use it again to write the object
// data.
ser.WriteStartObject(writer, p);
ser.WriteObjectContent(writer, p);
// Use the serializer to write the end of the
// object data. Then use the writer to write the end
// of the document.
ser.WriteEndObject(writer);
writer.WriteEndDocument();
Console.WriteLine("Done");
// Close and release the writer resources.
writer.Flush();
fs.Flush();
fs.Close();
}
NotInheritable Public Class ShowWriteStartObject
Public Shared Sub WriteObjectData(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 System.Runtime.Serialization.NetDataContractSerializer()
' Use the writer to start a document.
writer.WriteStartDocument(True)
' Use the serializer to write the start of the
' object data. Use it again to write the object
' data.
ser.WriteStartObject(writer, p)
writer.WriteStartAttribute("MyAttribute")
writer.WriteString("My Text")
writer.WriteEndAttribute()
ser.WriteObjectContent(writer, p)
' Use the serializer to write the end of the
' object data. Then use the writer to write the end
' of the document.
ser.WriteEndObject(writer)
writer.WriteEndDocument()
Console.WriteLine("Done")
' Close and release the writer resources.
writer.Flush()
fs.Flush()
fs.Close()
End Sub
Gäller för
WriteEndObject(XmlWriter)
Skriver det avslutande XML-elementet med hjälp av en XmlWriter.
public:
override void WriteEndObject(System::Xml::XmlWriter ^ writer);
public override void WriteEndObject(System.Xml.XmlWriter writer);
override this.WriteEndObject : System.Xml.XmlWriter -> unit
Public Overrides Sub WriteEndObject (writer As XmlWriter)
Parametrar
Undantag
är inställt på writernull.