Freigeben über


XmlSerializerNamespaces-Konstruktor ()

Initialisiert eine neue Instanz der XmlSerializerNamespaces-Klasse.

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

Syntax

'Declaration
Public Sub New
'Usage
Dim instance As New XmlSerializerNamespaces
public XmlSerializerNamespaces ()
public:
XmlSerializerNamespaces ()
public XmlSerializerNamespaces ()
public function XmlSerializerNamespaces ()

Beispiel

Im folgenden Beispiel wird eine Instanz der XmlSerializerNamespaces-Klasse erstellt, und dem Objekt wird ein aus einem Präfix und einem Namespace bestehendes Paar hinzugefügt.

Private Sub CreateBook(filename As String)
   Try
      ' Create instance of XmlSerializerNamespaces and add the namespaces.
      Dim myNameSpaces As New XmlSerializerNamespaces()
      myNameSpaces.Add("BookName", "http://www.cpandl.com")

      ' Create instance of XmlSerializer and specify the type of object;
      ' to be serialized.
      Dim mySerializerObject As New XmlSerializer(GetType(MyBook))

      Dim myWriter = New StreamWriter(filename)
      ' Create object to be serialized.
      Dim myXMLBook As New MyBook()

      myXMLBook.Author = "XMLAuthor"
      myXMLBook.BookName = "DIG THE XML"
      myXMLBook.Description = "This is a XML Book"

      Dim myBookPrice As New MyPriceClass()
      myBookPrice.Price = CDec(45.89)
      myBookPrice.Units = "$"
      myXMLBook.BookPrice = myBookPrice

      ' Serialize the object.
      mySerializerObject.Serialize(myWriter, myXMLBook, myNameSpaces)
      myWriter.Close()
   Catch e As Exception
      Console.WriteLine("Exception :" & e.Message & "Occured")
   End Try
End Sub
private void CreateBook(string filename)
{
   try
   {
      // Create instance of XmlSerializerNamespaces and add the namespaces.
      XmlSerializerNamespaces myNameSpaces = new XmlSerializerNamespaces();
      myNameSpaces.Add("BookName", "http://www.cpandl.com");
   
      // Create instance of XmlSerializer and specify the type of object
      // to be serialized.
      XmlSerializer mySerializerObject = 
         new XmlSerializer(typeof(MyBook));

      TextWriter myWriter = new StreamWriter(filename);
      // Create object to be serialized.
      MyBook myXMLBook = new MyBook();
   
      myXMLBook.Author = "XMLAuthor";
      myXMLBook.BookName = "DIG THE XML";
      myXMLBook.Description = "This is a XML Book";

      MyPriceClass myBookPrice = new MyPriceClass();
      myBookPrice.Price = (decimal) 45.89;
      myBookPrice.Units = "$";
      myXMLBook.BookPrice = myBookPrice;

      // Serialize the object.
      mySerializerObject.Serialize(myWriter, myXMLBook,myNameSpaces);
      myWriter.Close();
   }
   catch(Exception e)
   {
      Console.WriteLine("Exception :" + e.Message + "Occured");
   }
}
public:
   void CreateBook( String^ filename )
   {
      try
      {
         // Create instance of XmlSerializerNamespaces and add the namespaces.
         XmlSerializerNamespaces^ myNameSpaces = gcnew XmlSerializerNamespaces;
         myNameSpaces->Add( "BookName", "http://www.cpandl.com" );
         
         // Create instance of XmlSerializer and specify the type of object
         // to be serialized.
         XmlSerializer^ mySerializerObject =
            gcnew XmlSerializer( MyBook::typeid );

         TextWriter^ myWriter = gcnew StreamWriter( filename );
         // Create object to be serialized.
         MyBook^ myXMLBook = gcnew MyBook;

         myXMLBook->Author = "XMLAuthor";
         myXMLBook->BookName = "DIG THE XML";
         myXMLBook->Description = "This is a XML Book";

         MyPriceClass^ myBookPrice = gcnew MyPriceClass;
         myBookPrice->Price = (Decimal)45.89;
         myBookPrice->Units = "$";
         myXMLBook->BookPrice = myBookPrice;
         
         // Serialize the object.
         mySerializerObject->Serialize( myWriter, myXMLBook, myNameSpaces );
         myWriter->Close();
      }
      catch ( Exception^ e ) 
      {
         Console::WriteLine( "Exception: {0} occured", e->Message );
      }
   }
private void CreateBook(String fileName)
{
    try {
        // Create instance of XmlSerializerNamespaces and add the namespaces.
        XmlSerializerNamespaces myNameSpaces = new XmlSerializerNamespaces();
        myNameSpaces.Add("bookName", "http://www.cpandl.com");
        // Create instance of XmlSerializer and specify the type of object
        // to be serialized.
        XmlSerializer mySerializerObject = new XmlSerializer(
            MyBook.class.ToType());

        TextWriter myWriter = new StreamWriter(fileName);
        // Create object to be serialized.
        MyBook myXMLBook = new MyBook();

        myXMLBook.author = "XMLauthor";
        myXMLBook.bookName = "DIG THE XML";
        myXMLBook.description = "This is a XML Book";

        MyPriceClass mybookPrice = new MyPriceClass();
        mybookPrice.price = System.Convert.ToDecimal(45.89);
        mybookPrice.units = "$";
        myXMLBook.bookPrice = mybookPrice;
        // Serialize the object.
        mySerializerObject.Serialize(myWriter, myXMLBook, myNameSpaces);
        myWriter.Close();
    }
    catch (System.Exception e) {
        Console.WriteLine("Exception :" + e.get_Message() + "Occured");
    }
} //CreateBook

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

XmlSerializerNamespaces-Klasse
XmlSerializerNamespaces-Member
System.Xml.Serialization-Namespace