แก้ไข

แชร์ผ่าน


XmlAttributeAttribute Constructors

Definition

Initializes a new instance of the XmlAttributeAttribute class.

Overloads

XmlAttributeAttribute()

Initializes a new instance of the XmlAttributeAttribute class.

XmlAttributeAttribute(String)

Initializes a new instance of the XmlAttributeAttribute class and specifies the name of the generated XML attribute.

XmlAttributeAttribute(Type)

Initializes a new instance of the XmlAttributeAttribute class.

XmlAttributeAttribute(String, Type)

Initializes a new instance of the XmlAttributeAttribute class.

XmlAttributeAttribute()

Source:
XmlAttributeAttribute.cs
Source:
XmlAttributeAttribute.cs
Source:
XmlAttributeAttribute.cs

Initializes a new instance of the XmlAttributeAttribute class.

public:
 XmlAttributeAttribute();
public XmlAttributeAttribute ();
Public Sub New ()

Examples

#using <System.dll>
#using <System.xml.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 Student
{
public:
   String^ StudentName;
   int StudentNumber;
};

public ref class Book
{
public:
   String^ BookName;
   int BookNumber;
};

void SerializeObject( String^ studentFilename, String^ bookFilename )
{
   XmlSerializer^ mySerializer;
   TextWriter^ writer;

   // Create the XmlAttributeOverrides and XmlAttributes objects.
   XmlAttributeOverrides^ myXmlAttributeOverrides = gcnew XmlAttributeOverrides;
   XmlAttributes^ myXmlAttributes = gcnew XmlAttributes;

   /* Create an XmlAttributeAttribute set it to 
      the XmlAttribute property of the XmlAttributes object.*/
   XmlAttributeAttribute^ myXmlAttributeAttribute = gcnew XmlAttributeAttribute;
   myXmlAttributeAttribute->AttributeName = "Name";
   myXmlAttributes->XmlAttribute = myXmlAttributeAttribute;

   // Add to the XmlAttributeOverrides. Specify the member name.
   myXmlAttributeOverrides->Add( Student::typeid, "StudentName", myXmlAttributes );

   // Create the XmlSerializer.
   mySerializer = gcnew XmlSerializer( Student::typeid,myXmlAttributeOverrides );
   writer = gcnew StreamWriter( studentFilename );

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

   // Set the Name property, which will be generated as an XML attribute. 
   myStudent->StudentName = "James";
   myStudent->StudentNumber = 1;

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

   // Create the XmlAttributeOverrides and XmlAttributes objects.
   XmlAttributeOverrides^ myXmlBookAttributeOverrides = gcnew XmlAttributeOverrides;
   XmlAttributes^ myXmlBookAttributes = gcnew XmlAttributes;

   /* Create an XmlAttributeAttribute set it to 
      the XmlAttribute property of the XmlAttributes object.*/
   XmlAttributeAttribute^ myXmlBookAttributeAttribute = gcnew XmlAttributeAttribute( "Name" );
   myXmlBookAttributes->XmlAttribute = myXmlBookAttributeAttribute;

   // Add to the XmlAttributeOverrides. Specify the member name.
   myXmlBookAttributeOverrides->Add( Book::typeid, "BookName", myXmlBookAttributes );

   // Create the XmlSerializer.
   mySerializer = gcnew XmlSerializer( Book::typeid,myXmlBookAttributeOverrides );
   writer = gcnew StreamWriter( bookFilename );

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

   // Set the Name property, which will be generated as an XML attribute. 
   myBook->BookName = ".NET";
   myBook->BookNumber = 10;

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

int main()
{
   SerializeObject(  "Student.xml",  "Book.xml" );
}
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

// This is the class that will be serialized.
public class Student
{
   public string StudentName;
   public int StudentNumber;
}
 
public class Book
{
   public string BookName;
   public int BookNumber;
}
public class XMLAttributeAttribute_ctr1
{
   public static void Main()
   {
      XMLAttributeAttribute_ctr1 myAttribute = 
                                 new XMLAttributeAttribute_ctr1();
      myAttribute.SerializeObject("Student.xml","Book.xml");
   }
 
   public void SerializeObject(string studentFilename,string bookFilename)
   {
      XmlSerializer mySerializer;
      TextWriter writer;

      // Create the XmlAttributeOverrides and XmlAttributes objects.
      XmlAttributeOverrides myXmlAttributeOverrides = 
                                                     new XmlAttributeOverrides();
      XmlAttributes myXmlAttributes = new XmlAttributes();

      /* Create an XmlAttributeAttribute set it to 
      the XmlAttribute property of the XmlAttributes object.*/
      XmlAttributeAttribute myXmlAttributeAttribute = 
                                                new XmlAttributeAttribute();
      myXmlAttributeAttribute.AttributeName="Name";
      myXmlAttributes.XmlAttribute = myXmlAttributeAttribute;

      // Add to the XmlAttributeOverrides. Specify the member name.
      myXmlAttributeOverrides.Add(typeof(Student), "StudentName", 
                                                      myXmlAttributes);

      // Create the XmlSerializer.
      mySerializer = new  XmlSerializer(typeof(Student), 
                                                myXmlAttributeOverrides);
      
      writer = new StreamWriter(studentFilename);

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

      // Set the Name property, which will be generated as an XML attribute. 
      myStudent.StudentName= "James";
      myStudent.StudentNumber=1;
      // Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myStudent);
      writer.Close();

      // Create the XmlAttributeOverrides and XmlAttributes objects.
      XmlAttributeOverrides myXmlBookAttributeOverrides = 
                                                new XmlAttributeOverrides();
      XmlAttributes myXmlBookAttributes = new XmlAttributes();

      /* Create an XmlAttributeAttribute set it to 
      the XmlAttribute property of the XmlAttributes object.*/
      XmlAttributeAttribute myXmlBookAttributeAttribute = 
                                           new XmlAttributeAttribute("Name");
      myXmlBookAttributes.XmlAttribute = myXmlBookAttributeAttribute;

      // Add to the XmlAttributeOverrides. Specify the member name.
      myXmlBookAttributeOverrides.Add(typeof(Book), "BookName", 
                                             myXmlBookAttributes);

      // Create the XmlSerializer.
      mySerializer = new  XmlSerializer(typeof(Book), 
                                       myXmlBookAttributeOverrides);
      
      writer = new StreamWriter(bookFilename);

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

      // Set the Name property, which will be generated as an XML attribute. 
      myBook.BookName= ".NET";
      myBook.BookNumber=10;
      // Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myBook);
      writer.Close();
   }
}
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization

' This is the class that will be serialized.
Public Class Student
   Public StudentName As String
   Public StudentNumber As Integer
End Class

Public Class Book
   Public BookName As String
   Public BookNumber As Integer
End Class

Public Class XMLAttributeAttribute_ctr1

   Public Shared Sub Main()
      Dim myAttribute As New XMLAttributeAttribute_ctr1()
      myAttribute.SerializeObject("Student.xml", "Book.xml")
   End Sub

   Public Sub SerializeObject(studentFilename As String, bookFilename As String)
      Dim mySerializer As XmlSerializer
      Dim writer As TextWriter

      ' Create the XmlAttributeOverrides and XmlAttributes objects.
      Dim myXmlAttributeOverrides As New XmlAttributeOverrides()
      Dim myXmlAttributes As New XmlAttributes()

      ' Create an XmlAttributeAttribute set it to the XmlAttribute property of the XmlAttributes object.
      Dim myXmlAttributeAttribute As New XmlAttributeAttribute()
      myXmlAttributeAttribute.AttributeName = "Name"
      myXmlAttributes.XmlAttribute = myXmlAttributeAttribute

      ' Add to the XmlAttributeOverrides. Specify the member name.
      myXmlAttributeOverrides.Add(GetType(Student), "StudentName", myXmlAttributes)

      ' Create the XmlSerializer.
      mySerializer = New XmlSerializer(GetType(Student), myXmlAttributeOverrides)

      writer = New StreamWriter(studentFilename)

      ' Create an instance of the class that will be serialized.
      Dim myStudent As New Student()

      ' Set the Name property, which will be generated as an XML attribute. 
      myStudent.StudentName = "James"
      myStudent.StudentNumber = 1
      ' Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myStudent)
      writer.Close()

      ' Create the XmlAttributeOverrides and XmlAttributes objects.
      Dim myXmlBookAttributeOverrides As New XmlAttributeOverrides()
      Dim myXmlBookAttributes As New XmlAttributes()

      ' Create an XmlAttributeAttribute set it to the XmlAttribute property of the XmlAttributes object.
      Dim myXmlBookAttributeAttribute As New XmlAttributeAttribute("Name")
      myXmlBookAttributes.XmlAttribute = myXmlBookAttributeAttribute

      ' Add to the XmlAttributeOverrides. Specify the member name.
      myXmlBookAttributeOverrides.Add(GetType(Book), "BookName", myXmlBookAttributes)

      ' Create the XmlSerializer.
      mySerializer = New XmlSerializer(GetType(Book), myXmlBookAttributeOverrides)

      writer = New StreamWriter(bookFilename)

      ' Create an instance of the class that will be serialized.
      Dim myBook As New Book()

      ' Set the Name property, which will be generated as an XML attribute.
      myBook.BookName = ".NET"
      myBook.BookNumber = 10
      ' Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myBook)
      writer.Close()
   End Sub
End Class

Applies to

XmlAttributeAttribute(String)

Source:
XmlAttributeAttribute.cs
Source:
XmlAttributeAttribute.cs
Source:
XmlAttributeAttribute.cs

Initializes a new instance of the XmlAttributeAttribute class and specifies the name of the generated XML attribute.

public:
 XmlAttributeAttribute(System::String ^ attributeName);
public XmlAttributeAttribute (string attributeName);
public XmlAttributeAttribute (string? attributeName);
new System.Xml.Serialization.XmlAttributeAttribute : string -> System.Xml.Serialization.XmlAttributeAttribute
Public Sub New (attributeName As String)

Parameters

attributeName
String

The name of the XML attribute that the XmlSerializer generates.

Examples

#using <System.dll>
#using <System.xml.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 Student
{
public:
   String^ StudentName;
   int StudentNumber;
};

public ref class Book
{
public:
   String^ BookName;
   int BookNumber;
};

void SerializeObject( String^ studentFilename, String^ bookFilename )
{
   XmlSerializer^ mySerializer;
   TextWriter^ writer;

   // Create the XmlAttributeOverrides and XmlAttributes objects.
   XmlAttributeOverrides^ myXmlAttributeOverrides = gcnew XmlAttributeOverrides;
   XmlAttributes^ myXmlAttributes = gcnew XmlAttributes;

   /* Create an XmlAttributeAttribute set it to 
      the XmlAttribute property of the XmlAttributes object.*/
   XmlAttributeAttribute^ myXmlAttributeAttribute = gcnew XmlAttributeAttribute;
   myXmlAttributeAttribute->AttributeName = "Name";
   myXmlAttributes->XmlAttribute = myXmlAttributeAttribute;

   // Add to the XmlAttributeOverrides. Specify the member name.
   myXmlAttributeOverrides->Add( Student::typeid, "StudentName", myXmlAttributes );

   // Create the XmlSerializer.
   mySerializer = gcnew XmlSerializer( Student::typeid,myXmlAttributeOverrides );
   writer = gcnew StreamWriter( studentFilename );

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

   // Set the Name property, which will be generated as an XML attribute. 
   myStudent->StudentName = "James";
   myStudent->StudentNumber = 1;

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

   // Create the XmlAttributeOverrides and XmlAttributes objects.
   XmlAttributeOverrides^ myXmlBookAttributeOverrides = gcnew XmlAttributeOverrides;
   XmlAttributes^ myXmlBookAttributes = gcnew XmlAttributes;

   /* Create an XmlAttributeAttribute set it to 
      the XmlAttribute property of the XmlAttributes object.*/
   XmlAttributeAttribute^ myXmlBookAttributeAttribute = gcnew XmlAttributeAttribute( "Name" );
   myXmlBookAttributes->XmlAttribute = myXmlBookAttributeAttribute;

   // Add to the XmlAttributeOverrides. Specify the member name.
   myXmlBookAttributeOverrides->Add( Book::typeid, "BookName", myXmlBookAttributes );

   // Create the XmlSerializer.
   mySerializer = gcnew XmlSerializer( Book::typeid,myXmlBookAttributeOverrides );
   writer = gcnew StreamWriter( bookFilename );

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

   // Set the Name property, which will be generated as an XML attribute. 
   myBook->BookName = ".NET";
   myBook->BookNumber = 10;

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

int main()
{
   SerializeObject(  "Student.xml",  "Book.xml" );
}
using System;
using System.IO;
using System.Xml;
using System.Xml.Serialization;

// This is the class that will be serialized.
public class Student
{
   public string StudentName;
   public int StudentNumber;
}
 
public class Book
{
   public string BookName;
   public int BookNumber;
}
public class XMLAttributeAttribute_ctr1
{
   public static void Main()
   {
      XMLAttributeAttribute_ctr1 myAttribute = 
                                 new XMLAttributeAttribute_ctr1();
      myAttribute.SerializeObject("Student.xml","Book.xml");
   }
 
   public void SerializeObject(string studentFilename,string bookFilename)
   {
      XmlSerializer mySerializer;
      TextWriter writer;

      // Create the XmlAttributeOverrides and XmlAttributes objects.
      XmlAttributeOverrides myXmlAttributeOverrides = 
                                                     new XmlAttributeOverrides();
      XmlAttributes myXmlAttributes = new XmlAttributes();

      /* Create an XmlAttributeAttribute set it to 
      the XmlAttribute property of the XmlAttributes object.*/
      XmlAttributeAttribute myXmlAttributeAttribute = 
                                                new XmlAttributeAttribute();
      myXmlAttributeAttribute.AttributeName="Name";
      myXmlAttributes.XmlAttribute = myXmlAttributeAttribute;

      // Add to the XmlAttributeOverrides. Specify the member name.
      myXmlAttributeOverrides.Add(typeof(Student), "StudentName", 
                                                      myXmlAttributes);

      // Create the XmlSerializer.
      mySerializer = new  XmlSerializer(typeof(Student), 
                                                myXmlAttributeOverrides);
      
      writer = new StreamWriter(studentFilename);

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

      // Set the Name property, which will be generated as an XML attribute. 
      myStudent.StudentName= "James";
      myStudent.StudentNumber=1;
      // Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myStudent);
      writer.Close();

      // Create the XmlAttributeOverrides and XmlAttributes objects.
      XmlAttributeOverrides myXmlBookAttributeOverrides = 
                                                new XmlAttributeOverrides();
      XmlAttributes myXmlBookAttributes = new XmlAttributes();

      /* Create an XmlAttributeAttribute set it to 
      the XmlAttribute property of the XmlAttributes object.*/
      XmlAttributeAttribute myXmlBookAttributeAttribute = 
                                           new XmlAttributeAttribute("Name");
      myXmlBookAttributes.XmlAttribute = myXmlBookAttributeAttribute;

      // Add to the XmlAttributeOverrides. Specify the member name.
      myXmlBookAttributeOverrides.Add(typeof(Book), "BookName", 
                                             myXmlBookAttributes);

      // Create the XmlSerializer.
      mySerializer = new  XmlSerializer(typeof(Book), 
                                       myXmlBookAttributeOverrides);
      
      writer = new StreamWriter(bookFilename);

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

      // Set the Name property, which will be generated as an XML attribute. 
      myBook.BookName= ".NET";
      myBook.BookNumber=10;
      // Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myBook);
      writer.Close();
   }
}
Imports System.IO
Imports System.Xml
Imports System.Xml.Serialization

' This is the class that will be serialized.
Public Class Student
   Public StudentName As String
   Public StudentNumber As Integer
End Class

Public Class Book
   Public BookName As String
   Public BookNumber As Integer
End Class

Public Class XMLAttributeAttribute_ctr1

   Public Shared Sub Main()
      Dim myAttribute As New XMLAttributeAttribute_ctr1()
      myAttribute.SerializeObject("Student.xml", "Book.xml")
   End Sub

   Public Sub SerializeObject(studentFilename As String, bookFilename As String)
      Dim mySerializer As XmlSerializer
      Dim writer As TextWriter

      ' Create the XmlAttributeOverrides and XmlAttributes objects.
      Dim myXmlAttributeOverrides As New XmlAttributeOverrides()
      Dim myXmlAttributes As New XmlAttributes()

      ' Create an XmlAttributeAttribute set it to the XmlAttribute property of the XmlAttributes object.
      Dim myXmlAttributeAttribute As New XmlAttributeAttribute()
      myXmlAttributeAttribute.AttributeName = "Name"
      myXmlAttributes.XmlAttribute = myXmlAttributeAttribute

      ' Add to the XmlAttributeOverrides. Specify the member name.
      myXmlAttributeOverrides.Add(GetType(Student), "StudentName", myXmlAttributes)

      ' Create the XmlSerializer.
      mySerializer = New XmlSerializer(GetType(Student), myXmlAttributeOverrides)

      writer = New StreamWriter(studentFilename)

      ' Create an instance of the class that will be serialized.
      Dim myStudent As New Student()

      ' Set the Name property, which will be generated as an XML attribute. 
      myStudent.StudentName = "James"
      myStudent.StudentNumber = 1
      ' Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myStudent)
      writer.Close()

      ' Create the XmlAttributeOverrides and XmlAttributes objects.
      Dim myXmlBookAttributeOverrides As New XmlAttributeOverrides()
      Dim myXmlBookAttributes As New XmlAttributes()

      ' Create an XmlAttributeAttribute set it to the XmlAttribute property of the XmlAttributes object.
      Dim myXmlBookAttributeAttribute As New XmlAttributeAttribute("Name")
      myXmlBookAttributes.XmlAttribute = myXmlBookAttributeAttribute

      ' Add to the XmlAttributeOverrides. Specify the member name.
      myXmlBookAttributeOverrides.Add(GetType(Book), "BookName", myXmlBookAttributes)

      ' Create the XmlSerializer.
      mySerializer = New XmlSerializer(GetType(Book), myXmlBookAttributeOverrides)

      writer = New StreamWriter(bookFilename)

      ' Create an instance of the class that will be serialized.
      Dim myBook As New Book()

      ' Set the Name property, which will be generated as an XML attribute.
      myBook.BookName = ".NET"
      myBook.BookNumber = 10
      ' Serialize the class, and close the TextWriter.
      mySerializer.Serialize(writer, myBook)
      writer.Close()
   End Sub
End Class

Applies to

XmlAttributeAttribute(Type)

Source:
XmlAttributeAttribute.cs
Source:
XmlAttributeAttribute.cs
Source:
XmlAttributeAttribute.cs

Initializes a new instance of the XmlAttributeAttribute class.

public:
 XmlAttributeAttribute(Type ^ type);
public XmlAttributeAttribute (Type type);
public XmlAttributeAttribute (Type? type);
new System.Xml.Serialization.XmlAttributeAttribute : Type -> System.Xml.Serialization.XmlAttributeAttribute
Public Sub New (type As Type)

Parameters

type
Type

The Type used to store the attribute.

Applies to

XmlAttributeAttribute(String, Type)

Source:
XmlAttributeAttribute.cs
Source:
XmlAttributeAttribute.cs
Source:
XmlAttributeAttribute.cs

Initializes a new instance of the XmlAttributeAttribute class.

public:
 XmlAttributeAttribute(System::String ^ attributeName, Type ^ type);
public XmlAttributeAttribute (string attributeName, Type type);
public XmlAttributeAttribute (string? attributeName, Type? type);
new System.Xml.Serialization.XmlAttributeAttribute : string * Type -> System.Xml.Serialization.XmlAttributeAttribute
Public Sub New (attributeName As String, type As Type)

Parameters

attributeName
String

The name of the XML attribute that is generated.

type
Type

The Type used to store the attribute.

Applies to