EN
Ce contenu n’est pas disponible dans votre langue. Voici la version anglaise.
1 sur 4 ont trouvé cela utile - Évaluez ce sujet

XmlSerializer.Serialize Method

This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.

Overload List

This member supports the .NET Framework infrastructure and is not intended to be used directly from your code.

[Visual Basic] Overloads Protected Overridable Sub Serialize(Object, XmlSerializationWriter)
[C#] protected virtual void Serialize(object, XmlSerializationWriter);
[C++] protected: virtual void Serialize(Object*, XmlSerializationWriter*);
[JScript] protected function Serialize(Object, XmlSerializationWriter);

Serializes the specified Object and writes the XML document to a file using the specified Stream.

[Visual Basic] Overloads Public Sub Serialize(Stream, Object)
[C#] public void Serialize(Stream, object);
[C++] public: void Serialize(Stream*, Object*);
[JScript] public function Serialize(Stream, Object);

Serializes the specified Object and writes the XML document to a file using the specified TextWriter.

[Visual Basic] Overloads Public Sub Serialize(TextWriter, Object)
[C#] public void Serialize(TextWriter, object);
[C++] public: void Serialize(TextWriter*, Object*);
[JScript] public function Serialize(TextWriter, Object);

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

[Visual Basic] Overloads Public Sub Serialize(XmlWriter, Object)
[C#] public void Serialize(XmlWriter, object);
[C++] public: void Serialize(XmlWriter*, Object*);
[JScript] public function Serialize(XmlWriter, Object);

Serializes the specified Object and writes the XML document to a file using the specified Stream, referencing the specified namespaces.

[Visual Basic] Overloads Public Sub Serialize(Stream, Object, XmlSerializerNamespaces)
[C#] public void Serialize(Stream, object, XmlSerializerNamespaces);
[C++] public: void Serialize(Stream*, Object*, XmlSerializerNamespaces*);
[JScript] public function Serialize(Stream, Object, XmlSerializerNamespaces);

Serializes the specified Object and writes the XML document to a file using the specified TextWriter, referencing the specified namespaces.

[Visual Basic] Overloads Public Sub Serialize(TextWriter, Object, XmlSerializerNamespaces)
[C#] public void Serialize(TextWriter, object, XmlSerializerNamespaces);
[C++] public: void Serialize(TextWriter*, Object*, XmlSerializerNamespaces*);
[JScript] public function Serialize(TextWriter, Object, XmlSerializerNamespaces);

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

[Visual Basic] Overloads Public Sub Serialize(XmlWriter, Object, XmlSerializerNamespaces)
[C#] public void Serialize(XmlWriter, object, XmlSerializerNamespaces);
[C++] public: void Serialize(XmlWriter*, Object*, XmlSerializerNamespaces*);
[JScript] public function Serialize(XmlWriter, Object, XmlSerializerNamespaces);

Example

[Visual Basic, C#, C++] The following example serializes an object with an XmlWriter. The example also creates an XmlSerializerNamespaces and adds two namespaces to the object. Several instances of the XmlElementAttribute class are applied to the class members to specify the namespace for each element.

[Visual Basic, C#, C++] Note   This example shows how to use one of the overloaded versions of Serialize. For other examples that might be available, see the individual overload topics.
[Visual Basic] 
Imports System
Imports System.IO
Imports System.Text
Imports System.Xml
Imports System.Xml.Serialization


' This is the class that will be serialized.
Public Class OrderedItem
    <XmlElement(Namespace := "http://www.cpandl.com")> _
    Public ItemName As String
    
    <XmlElement(Namespace := "http://www.cpandl.com")> _
    Public Description As String
    
    <XmlElement(Namespace := "http://www.cohowinery.com")> _
    Public UnitPrice As Decimal
    
    <XmlElement(Namespace := "http://www.cpandl.com")> _
    Public Quantity As Integer
    
    <XmlElement(Namespace := "http://www.cohowinery.com")> _
    Public LineTotal As Decimal
    
    'A custom method used to calculate price per item.
    Public Sub Calculate()
        LineTotal = UnitPrice * Quantity
    End Sub
End Class


Public Class Test
    
    Public Shared Sub Main()
        Dim t As New Test()
        ' Write a purchase order.
        t.SerializeObject("simple.xml")
    End Sub    
    
    Private Sub SerializeObject(ByVal filename As String)
        Console.WriteLine("Writing With XmlTextWriter")
        
        Dim serializer As New XmlSerializer(GetType(OrderedItem))
        Dim i As New OrderedItem()
        With i
            .ItemName = "Widget"
            .Description = "Regular Widget"
            .Quantity = 10
            .UnitPrice = CDec(2.3)
            .Calculate()
        End With
        
        ' Create an XmlSerializerNamespaces object.
        Dim ns As New XmlSerializerNamespaces()
        ' Add two namespaces with prefixes.
        ns.Add("inventory", "http://www.cpandl.com")
        ns.Add("money", "http://www.cohowinery.com")
        ' Create an XmlTextWriter using a FileStream.
        Dim fs As New FileStream(filename, FileMode.Create)
        Dim writer As New XmlTextWriter(fs, New UTF8Encoding())
        ' Serialize using the XmlTextWriter.
        serializer.Serialize(writer, i, ns)
        writer.Close()
    End Sub
End Class


[C#] 
using System;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;

// This is the class that will be serialized.
public class OrderedItem
{
   [XmlElement(Namespace = "http://www.cpandl.com")]
   public string ItemName;
   [XmlElement(Namespace = "http://www.cpandl.com")]
   public string Description;
   [XmlElement(Namespace="http://www.cohowinery.com")]
   public decimal UnitPrice;
   [XmlElement(Namespace = "http://www.cpandl.com")]
   public int Quantity;
   [XmlElement(Namespace="http://www.cohowinery.com")]
   public decimal LineTotal;
   // A custom method used to calculate price per item.
   public void Calculate()
   {
      LineTotal = UnitPrice * Quantity;
   }
}
 
public class Test{
   public static void Main()
   {
      Test t = new Test();
   // Write a purchase order.
   t.SerializeObject("simple.xml");
   }
 
   private void SerializeObject(string filename)
   {
      Console.WriteLine("Writing With XmlTextWriter");

      XmlSerializer serializer = 
      new XmlSerializer(typeof(OrderedItem));
      OrderedItem i = new OrderedItem();
      i.ItemName = "Widget";
      i.Description = "Regular Widget";
      i.Quantity = 10;
      i.UnitPrice = (decimal) 2.30;
      i.Calculate();
 
      // Create an XmlSerializerNamespaces object.
      XmlSerializerNamespaces ns = 
      new XmlSerializerNamespaces();
      // Add two namespaces with prefixes.
      ns.Add("inventory", "http://www.cpandl.com");
      ns.Add("money", "http://www.cohowinery.com");
      // Create an XmlTextWriter using a FileStream.
      Stream fs = new FileStream(filename, FileMode.Create);
      XmlWriter writer = 
      new XmlTextWriter(fs, new UTF8Encoding());
      // Serialize using the XmlTextWriter.
      serializer.Serialize(writer, i, ns);
      writer.Close();
   }
}


[C++] 
#using <mscorlib.dll>
#using <System.Xml.dll>
using namespace System;
using namespace System::IO;
using namespace System::Text;
using namespace System::Xml;
using namespace System::Xml::Serialization;

// This is the class that will be serialized.
public __gc class OrderedItem
{
public:
   [XmlElement(Namespace = S"http://www.cpandl.com")]
   String* ItemName;
   [XmlElement(Namespace = S"http://www.cpandl.com")]
   String* Description;
   [XmlElement(Namespace=S"http://www.cohowinery.com")]
   Decimal UnitPrice;
   [XmlElement(Namespace = S"http://www.cpandl.com")]
   int Quantity;
   [XmlElement(Namespace=S"http://www.cohowinery.com")]
   Decimal LineTotal;
   // A custom method used to calculate price per item.
   void Calculate()
   {
      LineTotal = UnitPrice * Quantity;
   }
};

void SerializeObject(String* filename)
{
   Console::WriteLine(S"Writing With XmlTextWriter");

   XmlSerializer* serializer = 
      new XmlSerializer(__typeof(OrderedItem));
   OrderedItem* i = new OrderedItem();
   i->ItemName = S"Widget";
   i->Description = S"Regular Widget";
   i->Quantity = 10;
   i->UnitPrice = (Decimal) 2.30;
   i->Calculate();

   // Create an XmlSerializerNamespaces object.
   XmlSerializerNamespaces* ns = 
      new XmlSerializerNamespaces();
   // Add two namespaces with prefixes.
   ns->Add(S"inventory", S"http://www.cpandl.com");
   ns->Add(S"money", S"http://www.cohowinery.com");
   // Create an XmlTextWriter using a FileStream.
   Stream* fs = new FileStream(filename, FileMode::Create);
   XmlWriter* writer = 
      new XmlTextWriter(fs, new UTF8Encoding());
   // Serialize using the XmlTextWriter.
   serializer->Serialize(writer, i, ns);
   writer->Close();
}

int main()
{
   // Write a purchase order.
   SerializeObject(S"simple.xml");
}

<?xml version="1.0"?>
 <OrderedItem xmlns:inventory="http://www.cpandl.com" xmlns:money="http://www.cohowinery.com">
   <inventory:ItemName>Widget</inventory:ItemName>
   <inventory:Description>Regular Widget</inventory:Description>
   <money:UnitPrice>2.3</money:UnitPrice>
   <inventory:Quantity>10</inventory:Quantity>
   <money:LineTotal>23</money:LineTotal>
 </OrderedItem>

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

XmlSerializer Class | XmlSerializer Members | System.Xml.Serialization Namespace

Cela vous a-t-il été utile ?
(1500 caractères restants)
Merci pour vos suggestions.
Afficher:
© 2014 Microsoft. Tous droits réservés.