XmlAttributes.XmlAttribute 属性
定义
获取或设置一个对象,该对象指定 XmlSerializer 将公共字段或公共读/写属性串行化为 XML 特性的方式。Gets or sets an object that specifies how the XmlSerializer serializes a public field or public read/write property as an XML attribute.
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
属性值
控制将公共字段或读/写属性序列化为 XML 特性的 XmlAttributeAttribute。An XmlAttributeAttribute that controls the serialization of a public field or read/write property as an XML attribute.
示例
下面的示例序列化一个名为 Group 的类,该类包含一个名为的属性 GroupName ; 该 GroupName 属性序列化为 XML 特性。The following example serializes a class named Group that contains a property named GroupName; the GroupName property is serialized as an XML attribute. 该示例创建一个 XmlAttributeOverrides 和一个 XmlAttributes 对象,用于重写字段的默认序列化。The example creates an XmlAttributeOverrides and an XmlAttributes object to override the default serialization of the field. 然后,该示例创建一个 XmlAttributeAttribute 专门重写属性的,并将对象设置为属性,并将 XmlAttribute XmlAttributes 对象添加到 XmlAttributeOverrides 具有指定的重写成员名称的对象。The example then creates an XmlAttributeAttribute to specifically override the property, and the object is set to the XmlAttribute property The XmlAttributes object is added to the XmlAttributeOverrides object with the name of the overridden member specified. 最后, XmlSerializer 使用对象构造并返回 XmlAttributeOverrides 。Finally, an XmlSerializer is constructed and returned 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:
// This is the attribute that will be overridden.
[XmlAttributeAttribute]
String^ GroupName;
int GroupNumber;
};
// Return an XmlSerializer used for overriding.
XmlSerializer^ CreateOverrider()
{
// Create the XmlAttributeOverrides and XmlAttributes objects.
XmlAttributeOverrides^ xOver = gcnew XmlAttributeOverrides;
XmlAttributes^ xAttrs = gcnew XmlAttributes;
/* Create an overriding XmlAttributeAttribute set it to
the XmlAttribute property of the XmlAttributes object.*/
XmlAttributeAttribute^ xAttribute = gcnew XmlAttributeAttribute( "SplinterName" );
xAttrs->XmlAttribute = xAttribute;
// Add to the XmlAttributeOverrides. Specify the member name.
xOver->Add( Group::typeid, "GroupName", 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 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();
}
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->GroupNumber );
}
int main()
{
SerializeObject( "OverrideAttribute.xml" );
DeserializeObject( "OverrideAttribute.xml" );
}
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
注解
默认情况下,如果没有属性应用于公共字段或公共读/写属性,则会将其序列化为 XML 元素。By default, if no attribute is applied to a public field or public read/write property, it is serialized as an XML element. 还可以 XmlSerializer 通过 XmlAttributeAttribute 将应用于字段或属性来指示生成 XML 特性。You can also instruct the XmlSerializer to generate an XML attribute by applying an XmlAttributeAttribute to the field or property.
XmlAttribute属性允许你重写默认序列化,以及通过将应用到成员来控制的序列化 XmlAttributeAttribute 。The XmlAttribute property allows you to override the default serialization, as well as the serialization controlled by applying an XmlAttributeAttribute to the member. 为此,请创建一个 XmlAttributeAttribute 并将其属性设置 (如 AttributeName) 。To do this, create an XmlAttributeAttribute and set its properties (such as AttributeName). 将新的对象分配给 XmlAttribute 对象的属性 XmlAttributes 。Assign the new object to the XmlAttribute property of an XmlAttributes object. 将 XmlAttributes 对象添加到 XmlAttributeOverrides 对象,并指定包含该字段或属性的对象的类型,以及该字段或属性的名称。Add the XmlAttributes object to an XmlAttributeOverrides object and specify the type of the object that contains the field or property, and the name of the field or property. 最后, XmlSerializer 使用对象创建, XmlAttributeOverrides 并调用 Serialize 或 Deserialize 方法。Lastly, create an XmlSerializer using the XmlAttributeOverrides object and call the Serialize or Deserialize method.