XmlAttributes.XmlText XmlAttributes.XmlText XmlAttributes.XmlText XmlAttributes.XmlText Property

Definition

Ruft ein Objekt ab, mit dem XmlSerializer angewiesen wird, ein öffentliches Feld oder eine öffentliche Lese-/Schreibeigenschaft als XML-Text zu serialisieren, oder legt dieses fest.Gets or sets an object that instructs the XmlSerializer to serialize a public field or public read/write property as XML text.

public:
 property System::Xml::Serialization::XmlTextAttribute ^ XmlText { System::Xml::Serialization::XmlTextAttribute ^ get(); void set(System::Xml::Serialization::XmlTextAttribute ^ value); };
public System.Xml.Serialization.XmlTextAttribute XmlText { get; set; }
member this.XmlText : System.Xml.Serialization.XmlTextAttribute with get, set
Public Property XmlText As XmlTextAttribute

Eigenschaftswert

Ein XmlTextAttribute, das die Standardserialisierung öffentlicher Eigenschaften oder Felder überschreibt.An XmlTextAttribute that overrides the default serialization of a public property or field.

Beispiele

Im folgenden Beispiel wird die Klasse, die mit dem Namen serialisiert Group, enthält ein Feld namens Comment.The following example serializes the class named Group, which contains a field named Comment. Zum Überschreiben des Standardwerts Weise XmlSerializer serialisiert das Feld, das Beispiel erstellt eine XmlAttributeOverrides und XmlAttributes Objekt.To override the default way the XmlSerializer serializes the field, the example creates an XmlAttributeOverrides and an XmlAttributes object. Im Beispiel wird dann erstellt ein XmlTextAttribute -Objekt, das weist auf die XmlText -Eigenschaft, und fügt die XmlAttributes (mit dem Namen des Felds als XML-Text serialisiert werden soll)-Objekt der XmlAttributeOverrides Objekt.The example then creates an XmlTextAttribute object, which it assigns to the XmlText property, and adds the XmlAttributes object (with the name of the field to be serialized as XML text) to the XmlAttributeOverrides object. Und schließlich das Beispiel erstellt eine XmlSerializer mithilfe der XmlAttributeOverrides Objekt.Lastly the example creates an XmlSerializer using the XmlAttributeOverrides object.

#using <System.Xml.dll>
#using <System.dll>

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

// This is the class that will be serialized.
public ref class Group
{
public:
   String^ GroupName;

   // This field will be serialized as XML text. 
   String^ Comment;
};

// Return an XmlSerializer to be used for overriding. 
XmlSerializer^ CreateOverrider()
{
   // Create the XmlAttributeOverrides and XmlAttributes objects.
   XmlAttributeOverrides^ xOver = gcnew XmlAttributeOverrides;
   XmlAttributes^ xAttrs = gcnew XmlAttributes;

   /* Create an XmlTextAttribute and assign it to the XmlText 
      property. This instructs the XmlSerializer to treat the 
      Comment field as XML text. */
   XmlTextAttribute^ xText = gcnew XmlTextAttribute;
   xAttrs->XmlText = xText;
   xOver->Add( Group::typeid, "Comment", xAttrs );

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

void SerializeObject( String^ filename )
{
   // Create an instance of the XmlSerializer class.
   XmlSerializer^ mySerializer = CreateOverrider();

   // Writing the file requires a TextWriter.
   TextWriter^ writer = gcnew StreamWriter( filename );

   // Create an instance of the class that will be serialized.
   Group^ myGroup = gcnew Group;

   // Set the object properties.
   myGroup->GroupName = ".NET";
   myGroup->Comment = "Great Stuff!";

   // Serialize the class, and close the TextWriter.
   mySerializer->Serialize( writer, myGroup );
   writer->Close();
}

void DeserializeObject( String^ filename )
{
   XmlSerializer^ mySerializer = CreateOverrider();
   FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
   Group^ myGroup = dynamic_cast<Group^>(mySerializer->Deserialize( fs ));
   Console::WriteLine( myGroup->GroupName );
   Console::WriteLine( myGroup->Comment );
}

int main()
{
   SerializeObject( "OverrideText.xml" );
   DeserializeObject( "OverrideText.xml" );
}
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

// This is the class that will be serialized.
public class Group
{
   public string GroupName;

   // This field will be serialized as XML text. 
   public string Comment;
}
 
public class Run
{
   public static void Main()
   {
      Run test = new Run();
      test.SerializeObject("OverrideText.xml");
      test.DeserializeObject("OverrideText.xml");
   }

   // Return an XmlSerializer to be used for overriding. 
   public XmlSerializer CreateOverrider()
   {
      // Create the XmlAttributeOverrides and XmlAttributes objects.
      XmlAttributeOverrides xOver = new XmlAttributeOverrides();
      XmlAttributes xAttrs = new XmlAttributes();

      /* Create an XmlTextAttribute and assign it to the XmlText 
      property. This instructs the XmlSerializer to treat the 
      Comment field as XML text. */      
      XmlTextAttribute xText = new XmlTextAttribute();
      xAttrs.XmlText = xText;
      xOver.Add(typeof(Group), "Comment", xAttrs);

      // Create the XmlSerializer, and return it.
      return new XmlSerializer(typeof(Group), xOver);
   }
   
 
   public void SerializeObject(string filename)
   {
      // Create an instance of the XmlSerializer class.
      XmlSerializer mySerializer =  CreateOverrider();
      // Writing the file requires a TextWriter.
      TextWriter writer = new StreamWriter(filename);

      // Create an instance of the class that will be serialized.
      Group myGroup = new Group();

      // Set the object properties.
      myGroup.GroupName = ".NET";
      myGroup.Comment = "Great Stuff!";      
      // Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myGroup);
      writer.Close();
   }

   public void DeserializeObject(string filename)
   {
      XmlSerializer mySerializer = CreateOverrider();
      FileStream fs = new FileStream(filename, FileMode.Open);
      Group myGroup = (Group) 
      mySerializer.Deserialize(fs);
      Console.WriteLine(myGroup.GroupName);
      Console.WriteLine(myGroup.Comment);
   }
}
   
Imports System
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization


' This is the class that will be serialized.
Public Class Group
    Public GroupName As String
    
    ' This field will be serialized as XML text. 
    Public Comment As String
End Class


Public Class Run
    
    Public Shared Sub Main()
        Dim test As New Run()
        test.SerializeObject("OverrideText.xml")
        test.DeserializeObject("OverrideText.xml")
    End Sub

        
    ' Return an XmlSerializer to be used for overriding. 
    Public Function CreateOverrider() As XmlSerializer
        ' Create the XmlAttributeOverrides and XmlAttributes objects.
        Dim xOver As New XmlAttributeOverrides()
        Dim xAttrs As New XmlAttributes()
        
        ' Create an XmlTextAttribute and assign it to the XmlText
        ' property. This instructs the XmlSerializer to treat the
        ' Comment field as XML text. 
        Dim xText As New XmlTextAttribute()
        xAttrs.XmlText = xText
        xOver.Add(GetType(Group), "Comment", xAttrs)
        
        ' Create the XmlSerializer, and return it.
        Return New XmlSerializer(GetType(Group), xOver)
    End Function
    
    
    
    Public Sub SerializeObject(ByVal filename As String)
        ' Create an instance of the XmlSerializer class.
        Dim mySerializer As XmlSerializer = CreateOverrider()
        ' Writing the file requires a TextWriter.
        Dim writer As New StreamWriter(filename)
        
        ' Create an instance of the class that will be serialized.
        Dim myGroup As New Group()
        
        ' Set the object properties.
        myGroup.GroupName = ".NET"
        myGroup.Comment = "Great Stuff!"
        ' Serialize the class, and close the TextWriter.
        mySerializer.Serialize(writer, myGroup)
        writer.Close()
    End Sub
    
    
    Public Sub DeserializeObject(ByVal filename As String)
        Dim mySerializer As XmlSerializer = CreateOverrider()
        Dim fs As New FileStream(filename, FileMode.Open)
        Dim myGroup As Group = CType(mySerializer.Deserialize(fs), Group)
        Console.WriteLine(myGroup.GroupName)
        Console.WriteLine(myGroup.Comment)
    End Sub
End Class

Hinweise

Standardmäßig ein öffentliches Feld oder eine öffentliche Lese-/Schreibeigenschaft als XML-Element von serialisiert ist die XmlSerializer.By default, a public field or public read/write property is serialized as an XML element by the XmlSerializer. Es kann jedoch erzwingen, das Feld oder die Eigenschaft, die als XML-Text serialisiert werden, durch Anwenden einer XmlTextAttribute dem Feld oder Eigenschaft.However, it can force the field or property to be serialized as XML text by applying an XmlTextAttribute to the field or property.

Hinweis

Die XmlTextAttribute kann nicht angewendet werden, um ein Feld oder eine Eigenschaft, die ein Array zurückgibt.The XmlTextAttribute cannot be applied to a field or property that returns an array.

Zum Überschreiben der Standardserialisierung eines Felds oder Eigenschaft an (das Array nicht zurückgeben) erstellen Sie eine XmlTextAttribute und weisen sie Sie der XmlText Eigenschaft eine XmlAttributes Objekt.To override the default serialization of a field or property (that does not return an array), create an XmlTextAttribute and assign it to the XmlText property of an XmlAttributes object. Hinzufügen der XmlAttributes -Objekt an eine XmlAttributeOverrides Objekt, und geben Sie den Typ des Objekts, das außer Kraft gesetzte Feld oder Eigenschaft enthält, und der Name der überschriebenen Felds oder der Eigenschaft.Add the XmlAttributes object to an XmlAttributeOverrides object and specify the type of the object that contains the overridden field or property, and the name of the overridden field or property.

Gilt für: