Freigeben über


XmlTextAttribute-Konstruktor ()

Initialisiert eine neue Instanz der XmlTextAttribute-Klasse.

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

Syntax

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

Hinweise

Sie können die Art, in der XmlSerializer ein öffentliches Feld oder eine öffentliche Lese-/Schreib-Eigenschaft serialisiert, überschreiben, indem Sie XmlAttributes erstellen und die entsprechende XmlText-Eigenschaft auf XmlTextAttribute festlegen. Weitere Informationen finden Sie unter der XmlAttributeOverrides-Klasse.

Beispiel

Im folgenden Beispiel wird eine Klasse serialisiert, die das öffentliche Feld Comment enthält. Im Beispiel wird ein XmlTextAttribute auf das Feld angewendet und dabei die Serialisierung als XML-Element überschrieben, sodass es als XML-Text serialisiert wird.

Imports System
Imports System.IO
Imports System.Xml.Serialization
Imports System.Xml.Schema
Imports System.Xml


Public Class Group
    Public GroupName As String
    Public Comment As String
End Class

Public Class Test
    
    Public Shared Sub Main()
        Dim t As New Test()
        t.SerializerOrder("TextOverride.xml")
    End Sub
    
    ' Create an instance of the XmlSerializer class that overrides
    ' the default way it serializes an object. 
    Public Function CreateOverrider() As XmlSerializer
        ' CreatE instances of the XmlAttributes and
        ' XmlAttributeOverrides classes.
        
        Dim attrs As New XmlAttributes()        
        Dim xOver As New XmlAttributeOverrides()
        
        ' Create an XmlTextAttribute to override the default
        ' serialization process. 
        Dim xText As New XmlTextAttribute()
        attrs.XmlText = xText
        
        ' Add the XmlAttributes to the XmlAttributeOverrides.
        xOver.Add(GetType(Group), "Comment", attrs)
        
        ' Create the XmlSerializer, and return it.
        Dim xSer As New XmlSerializer(GetType(Group), xOver)
        Return xSer
    End Function
    
    
    Public Sub SerializerOrder(ByVal filename As String)
        ' Create an XmlSerializer instance.
        Dim xSer As XmlSerializer = CreateOverrider()
        
        ' Create the object and serialize it.
        Dim myGroup As New Group()
        myGroup.Comment = "This is a great product."
        
        Dim writer As New StreamWriter(filename)
        xSer.Serialize(writer, myGroup)
    End Sub
End Class
using System;
using System.IO;
using System.Xml.Serialization;
using System.Xml.Schema;
using System.Xml;


public class Group {
    public string GroupName;
    public string Comment;
}

public class Test {
    public static void Main() {
        Test t = new Test();
        t.SerializerOrder("TextOverride.xml");
    }
    /* Create an instance of the XmlSerializer class that overrides 
       the default way it serializes an object. */
    public XmlSerializer CreateOverrider() {
        /* Create instances of the XmlAttributes and 
        XmlAttributeOverrides classes. */
   
        XmlAttributes attrs = new XmlAttributes();

        XmlAttributeOverrides xOver = new XmlAttributeOverrides();
              
        /* Create an XmlTextAttribute to override the default 
        serialization process. */
        XmlTextAttribute xText = new XmlTextAttribute();
        attrs.XmlText = xText;

        // Add the XmlAttributes to the XmlAttributeOverrides.
        xOver.Add(typeof(Group), "Comment", attrs);

        // Create the XmlSerializer, and return it.
        XmlSerializer xSer = new XmlSerializer(typeof(Group), xOver);
        return xSer;
    }

    public void SerializerOrder(string filename) {
        // Create an XmlSerializer instance.
        XmlSerializer xSer = CreateOverrider();

        // Create the object and serialize it.
        Group myGroup = new Group();
        myGroup.Comment = "This is a great product.";
      
        TextWriter writer = new StreamWriter(filename);
        xSer.Serialize(writer, myGroup);
    }
}
   
#using <System.Xml.dll>
#using <System.dll>

using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;
using namespace System::Xml::Schema;

public ref class Group
{
public:
   String^ GroupName;
   String^ Comment;
};

public ref class Test
{
public:
   static void main()
   {
      Test^ t = gcnew Test;
      t->SerializerOrder( "TextOverride.xml" );
   }

   /* Create an instance of the XmlSerializer class that overrides 
          the default way it serializes an object. */
   XmlSerializer^ CreateOverrider()
   {
      /* Create instances of the XmlAttributes and 
              XmlAttributeOverrides classes. */
      XmlAttributes^ attrs = gcnew XmlAttributes;
      XmlAttributeOverrides^ xOver = gcnew XmlAttributeOverrides;

      /* Create an XmlTextAttribute to override the default 
              serialization process. */
      XmlTextAttribute^ xText = gcnew XmlTextAttribute;
      attrs->XmlText = xText;

      // Add the XmlAttributes to the XmlAttributeOverrides.
      xOver->Add( Group::typeid, "Comment", attrs );

      // Create the XmlSerializer, and return it.
      XmlSerializer^ xSer = gcnew XmlSerializer( Group::typeid,xOver );
      return xSer;
   }

   void SerializerOrder( String^ filename )
   {
      // Create an XmlSerializer instance.
      XmlSerializer^ xSer = CreateOverrider();

      // Create the object and serialize it.
      Group^ myGroup = gcnew Group;
      myGroup->Comment = "This is a great product.";
      TextWriter^ writer = gcnew StreamWriter( filename );
      xSer->Serialize( writer, myGroup );
   }
};

int main()
{
   Test::main();
}
import System.*;
import System.IO.*;
import System.Xml.Serialization.*;
import System.Xml.Schema.*;
import System.Xml.*;

public class Group
{
    public String groupName;
    public String comment;
} //Group

public class Test
{
    public static void main(String[] args)
    {
        Test t = new Test();
        t.SerializerOrder("TextOverride.xml");
    } //main

    /* Create an instance of the XmlSerializer class that overrides 
       the default way it serializes an object. */
    public XmlSerializer CreateOverrider()
    {
        /* Create instances of the XmlAttributes and 
           XmlAttributeOverrides classes. */
        XmlAttributes attrs = new XmlAttributes();
        XmlAttributeOverrides xOver = new XmlAttributeOverrides();

        /* Create an XmlTextAttribute to override the default 
           serialization process. */
        XmlTextAttribute xText = new XmlTextAttribute();
        attrs.set_XmlText(xText);

        // Add the XmlAttributes to the XmlAttributeOverrides.
        xOver.Add(Group.class.ToType(), "comment", attrs);

        // Create the XmlSerializer, and return it.
        XmlSerializer xSer = new XmlSerializer(Group.class.ToType(), xOver);
        return xSer;
    } //CreateOverrider

    public void SerializerOrder(String filename)
    {
        // Create an XmlSerializer instance.
        XmlSerializer xSer = CreateOverrider();

        // Create the object and serialize it.
        Group myGroup = new Group();
        myGroup.comment = "This is a great product.";
        TextWriter writer = new StreamWriter(filename);
        xSer.Serialize(writer, myGroup);
    } //SerializerOrder
} //Test

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

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