XmlAttributes.XmlAttribute Własność
Definicja
Ważny
Niektóre informacje dotyczą produktów przedpremierowych, które mogą zostać znacznie zmodyfikowane przed premierą. Microsoft nie udziela żadnych gwarancji, ani wyraźnych, ani domniemanych, dotyczących informacji podanych tutaj.
Pobiera lub ustawia obiekt, który określa, jak XmlSerializer serializuje pole publiczne lub publiczną właściwość odczytu/zapisu jako atrybut XML.
public:
property System::Xml::Serialization::XmlAttributeAttribute ^ XmlAttribute { System::Xml::Serialization::XmlAttributeAttribute ^ get(); void set(System::Xml::Serialization::XmlAttributeAttribute ^ value); };
public System.Xml.Serialization.XmlAttributeAttribute XmlAttribute { get; set; }
public System.Xml.Serialization.XmlAttributeAttribute? XmlAttribute { get; set; }
member this.XmlAttribute : System.Xml.Serialization.XmlAttributeAttribute with get, set
Public Property XmlAttribute As XmlAttributeAttribute
Wartość nieruchomości
Element XmlAttributeAttribute , który kontroluje serializacji pola publicznego lub właściwości read/write jako atrybut XML.
Przykłady
Poniższy przykład serializuje klasę o nazwie , która zawiera właściwość o nazwie GroupGroupName; GroupName właściwość jest serializowana jako atrybut XML. Przykład tworzy obiekt XmlAttributeOverrides i, aby zastąpić domyślną XmlAttributes serializacji pola. W tym przykładzie zostanie utworzony obiekt XmlAttributeAttribute w celu zastąpienia właściwości, a obiekt jest ustawiony na XmlAttribute właściwość Obiekt XmlAttributes jest dodawany do XmlAttributeOverrides obiektu o nazwie określonego elementu członkowskiego zastąpienia.
XmlSerializer Na koniec obiekt jest konstruowany i zwracany przy użyciu XmlAttributeOverrides obiektu.
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
// This is the class that will be serialized.
public class Group
{
// This is the attribute that will be overridden.
[XmlAttribute]
public string GroupName;
public int GroupNumber;
}
public class Run
{
public static void Main()
{
Run test = new Run();
test.SerializeObject("OverrideAttribute.xml");
test.DeserializeObject("OverrideAttribute.xml");
}
// Return an XmlSerializer used for overriding.
public XmlSerializer CreateOverrider()
{
// Create the XmlAttributeOverrides and XmlAttributes objects.
XmlAttributeOverrides xOver = new XmlAttributeOverrides();
XmlAttributes xAttrs = new XmlAttributes();
/* Create an overriding XmlAttributeAttribute set it to
the XmlAttribute property of the XmlAttributes object.*/
XmlAttributeAttribute xAttribute = new XmlAttributeAttribute("SplinterName");
xAttrs.XmlAttribute = xAttribute;
// Add to the XmlAttributeOverrides. Specify the member name.
xOver.Add(typeof(Group), "GroupName", 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 Name property, which will be generated
as an XML attribute. */
myGroup.GroupName = ".NET";
myGroup.GroupNumber = 1;
// 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.GroupNumber);
}
}
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
' This is the class that will be serialized.
Public Class Group
' This is the attribute that will be overridden.
<XmlAttribute()> Public GroupName As String
Public GroupNumber As Integer
End Class
Public Class Run
Public Shared Sub Main()
Dim test As New Run()
test.SerializeObject("OverrideAttribute.xml")
test.DeserializeObject("OverrideAttribute.xml")
End Sub
' Return an XmlSerializer 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 overriding XmlAttributeAttribute set it to
' the XmlAttribute property of the XmlAttributes object.
Dim xAttribute As New XmlAttributeAttribute("SplinterName")
xAttrs.XmlAttribute = xAttribute
' Add to the XmlAttributeOverrides. Specify the member name.
xOver.Add(GetType(Group), "GroupName", xAttrs)
' Create the XmlSerializer and return it.
Return New XmlSerializer(GetType(Group), xOver)
End Function 'CreateOverrider
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 Name property, which will be generated
' as an XML attribute.
myGroup.GroupName = ".NET"
myGroup.GroupNumber = 1
' 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.GroupNumber)
End Sub
End Class
Uwagi
Domyślnie, jeśli żaden atrybut nie jest stosowany do pola publicznego lub publicznej właściwości odczytu/zapisu, jest serializowany jako element XML. Można również poinstruować, XmlSerializer aby wygenerować atrybut XML, stosując element XmlAttributeAttribute do pola lub właściwości.
Właściwość XmlAttribute umożliwia zastąpienie domyślnej serializacji, a także serializacji kontrolowanej przez zastosowanie elementu XmlAttributeAttribute członkowskiego. W tym celu utwórz XmlAttributeAttribute obiekt i ustaw jego właściwości (na przykład AttributeName). Przypisz nowy obiekt do XmlAttribute właściwości XmlAttributes obiektu. XmlAttributes Dodaj obiekt do XmlAttributeOverrides obiektu i określ typ obiektu zawierającego pole lub właściwość oraz nazwę pola lub właściwości. Na koniec utwórz XmlSerializer obiekt przy użyciu XmlAttributeOverrides obiektu i wywołaj metodę Serialize or Deserialize .