XmlAttributeAttribute.AttributeName Свойство
Важно!
Некоторые сведения относятся к предварительной версии продукта, в которую до выпуска могут быть внесены существенные изменения. Майкрософт не предоставляет никаких гарантий, явных или подразумеваемых, относительно приведенных здесь сведений.
Возвращает или задает имя XML-атрибута.
public:
property System::String ^ AttributeName { System::String ^ get(); void set(System::String ^ value); };
public string AttributeName { get; set; }
member this.AttributeName : string with get, set
Public Property AttributeName As String
Имя XML-атрибута. По умолчанию это имя члена.
В следующем примере задается AttributeName свойство объекта XmlAttributeAttribute.
#using <System.Xml.dll>
#using <System.dll>
using namespace System;
using namespace System::IO;
using namespace System::Xml;
using namespace System::Xml::Serialization;
public ref class Group
{
public:
// Change the XML attribute name.
[XmlAttributeAttribute(AttributeName="MgrName")]
String^ Name;
/* Use the AttributeName to collect all the XML attributes
in the XML-document instance. */
};
void SerializeObject( String^ filename )
{
Console::WriteLine( "Serializing" );
// Create an instance of the XmlSerializer class.
XmlSerializer^ mySerializer = gcnew XmlSerializer( Group::typeid );
// 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->Name = "Wallace";
// Serialize the class, and close the TextWriter.
mySerializer->Serialize( writer, myGroup );
writer->Close();
}
void DeserializeObject( String^ filename )
{
Console::WriteLine( "Deserializing" );
XmlSerializer^ mySerializer = gcnew XmlSerializer( Group::typeid );
FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
Group^ myGroup = dynamic_cast<Group^>(mySerializer->Deserialize( fs ));
Console::WriteLine( myGroup->Name );
}
int main()
{
/* To use the AttributeName to collect all the
XML attributes. Call SerializeObject to generate
an XML document and alter the document by adding
new XML attributes to it. Then comment out the SerializeObject
method, and call DeserializeObject. */
SerializeObject( "MyAtts.xml" );
DeserializeObject( "MyAtts.xml" );
}
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
public class Group
{
// Change the XML attribute name.
[XmlAttribute(AttributeName = "MgrName")]
public string Name;
/* Use the AttributeName to collect all the XML attributes
in the XML-document instance. */
}
public class Run
{
public static void Main()
{
Run test = new Run();
/* To use the AttributeName to collect all the
XML attributes. Call SerializeObject to generate
an XML document and alter the document by adding
new XML attributes to it. Then comment out the SerializeObject
method, and call DeserializeObject. */
test.SerializeObject("MyAtts.xml");
test.DeserializeObject("MyAtts.xml");
}
public void SerializeObject(string filename)
{
Console.WriteLine("Serializing");
// Create an instance of the XmlSerializer class.
XmlSerializer mySerializer = new XmlSerializer(typeof(Group));
// 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.Name = "Wallace";
// Serialize the class, and close the TextWriter.
mySerializer.Serialize(writer, myGroup);
writer.Close();
}
public void DeserializeObject(string filename)
{
Console.WriteLine("Deserializing");
XmlSerializer mySerializer =
new XmlSerializer(typeof(Group));
FileStream fs = new FileStream(filename, FileMode.Open);
Group myGroup = (Group)
mySerializer.Deserialize(fs);
Console.WriteLine(myGroup.Name);
}
}
Option Explicit
Option Strict
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization
Public Class Group
' Change the XML attribute name.
<XmlAttribute(AttributeName := "MgrName")> Public Name As String
' Use the AttributeName to collect all the XML attributes
' in the XML-document instance.
<XmlAttribute(AttributeName := "*")> Public Attrs() As XmlAttribute
End Class
Public Class Run
Public Shared Sub Main()
Dim test As New Run()
' To use the AttributeName to collect all the
' XML attributes. Call SerializeObject to generate
' an XML document and alter the document by adding
' new XML attributes to it. Then comment out the SerializeObject
' method, and call DeserializeObject.
test.SerializeObject("MyAtts.xml")
test.DeserializeObject("MyAtts.xml")
End Sub
Public Sub SerializeObject(ByVal filename As String)
Console.WriteLine("Serializing")
' Create an instance of the XmlSerializer class.
Dim mySerializer As New XmlSerializer(GetType(Group))
' 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.Name = "Wallace"
' Serialize the class, and close the TextWriter.
mySerializer.Serialize(writer, myGroup)
writer.Close()
End Sub
Public Sub DeserializeObject(ByVal filename As String)
Console.WriteLine("Deserializing")
Dim mySerializer As New XmlSerializer(GetType(Group))
Dim fs As New FileStream(filename, FileMode.Open)
Dim myGroup As Group = CType(mySerializer.Deserialize(fs), Group)
' The following code prints all the attributes in the
' XML document. To collect the attributes, the AttributeName of the
' XmlAttributeAttribute must be set to "*".
Dim xAtt As XmlAttribute
For Each xAtt In myGroup.Attrs
Console.WriteLine(xAtt.Name & ":" & xAtt.Value)
Next xAtt
End Sub
End Class
AttributeName Используйте свойство, чтобы указать имя атрибута XML, если значение по умолчанию не может использоваться. Например, если имя атрибута XML недопустимо в качестве идентификатора элемента, можно использовать допустимое имя идентификатора, задав AttributeName недопустимое имя.
Продукт | Версии |
---|---|
.NET | Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7 |
.NET Framework | 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8 |
.NET Standard | 2.0, 2.1 |
UWP | 10.0 |