XmlAttributes.XmlText Tulajdonság

Definíció

Lekéri vagy beállít egy objektumot, amely arra utasítja, XmlSerializer hogy szerializáljon egy nyilvános mezőt vagy egy nyilvános olvasási/írási tulajdonságot XML-szövegként.

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; }
public System.Xml.Serialization.XmlTextAttribute? XmlText { get; set; }
member this.XmlText : System.Xml.Serialization.XmlTextAttribute with get, set
Public Property XmlText As XmlTextAttribute

Tulajdonság értéke

Egy XmlTextAttribute nyilvános tulajdonság vagy mező alapértelmezett szerializálását felülbíráló.

Példák

Az alábbi példa szerializálja a nevesített Grouposztályt, amely egy nevesített Commentmezőt tartalmaz. A mező szerializálásának XmlSerializer alapértelmezett módjának felülbírálásához a példa létrehoz egy objektumot és egy XmlAttributeOverrides objektumot XmlAttributes . A példa ezután létrehoz egy XmlTextAttribute objektumot, amelyet hozzárendel a XmlText tulajdonsághoz, és hozzáadja az XmlAttributes objektumot (a szerializálandó mező nevével XML-szövegként) az XmlAttributeOverrides objektumhoz. Végül a példa létrehoz egy XmlSerializer objektumot XmlAttributeOverrides .

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.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

Megjegyzések

A nyilvános mező vagy a nyilvános olvasási/írási tulajdonság alapértelmezés szerint XML-elemként van szerializálva a XmlSerializer. Kényszerítheti azonban, hogy a mezőt vagy tulajdonságot XML-szövegként szerializálja, ha alkalmaz egy XmlTextAttribute mezőt vagy tulajdonságot.

Note

Nem XmlTextAttribute alkalmazható olyan mezőre vagy tulajdonságra, amely tömböt ad vissza.

Ha felül szeretné bírálni egy mező vagy tulajdonság alapértelmezett szerializálását (amely nem ad vissza tömböt), hozzon létre és XmlTextAttribute rendelje hozzá egy XmlTextXmlAttributes objektum tulajdonságához. Adja hozzá az XmlAttributes objektumot egy XmlAttributeOverrides objektumhoz, és adja meg annak az objektumnak a típusát, amely a felülírt mezőt vagy tulajdonságot tartalmazza, valamint a felülírt mező vagy tulajdonság nevét.

A következőre érvényes: