Freigeben über


XmlChoiceIdentifierAttribute.MemberName-Eigenschaft

Ruft den Namen des Felds ab, das die Enumeration zurückgibt, mit der Typen bestimmt werden, oder legt diesen fest.

Namespace: System.Xml.Serialization
Assembly: System.Xml (in system.xml.dll)

Syntax

'Declaration
Public Property MemberName As String
'Usage
Dim instance As XmlChoiceIdentifierAttribute
Dim value As String

value = instance.MemberName

instance.MemberName = value
public string MemberName { get; set; }
public:
property String^ MemberName {
    String^ get ();
    void set (String^ value);
}
/** @property */
public String get_MemberName ()

/** @property */
public void set_MemberName (String value)
public function get MemberName () : String

public function set MemberName (value : String)

Eigenschaftenwert

Der Name eines Felds, das eine Enumeration zurückgibt.

Hinweise

Mindestens ein Member muss in der Enumeration vorhanden sein, die von dem im MemberName-Wert benannten Feld zurückgegeben wird. In der Standardeinstellung nimmt die Enumeration den Namen des Felds an, auf das das XmlChoiceIdentifierAttribute angewendet wird.

Beispiel

Im folgenden Beispiel wird die Klasse Choices serialisiert, die die beiden Felder MyChoice und ManyChoices enthält. Das XmlChoiceIdentifierAttribute wird auf jedes Feld angewendet, das (über die MemberName-Eigenschaft) einen anderen Klassenmember angibt, der eine Enumeration abruft oder festlegt, die den Memberwert bestimmt. Das MyChoice-Feld kann mit einem entsprechenden Enumerationsmember aus dem EnumType-Feld auf einen einzelnen Wert festgelegt werden. Das ManyChoices-Feld gibt ein Array von Objekten zurück. Das ChoiceArray-Feld gibt ein Array von Enumerationswerten zurück. Für jeden Arraymember im ManyChoices-Feld ist ein entsprechender Member in dem Array vorhanden, das vom ChoiceArray-Feld zurückgegeben wird.

Imports System
Imports System.Xml
Imports System.Xml.Serialization
Imports System.IO

Public Class Choices
   ' The MyChoice field can be set to any one of 
   ' the types below. 
   <XmlChoiceIdentifier("EnumType"), _
   XmlElement("Word", GetType(String)), _
   XmlElement("Number", GetType(Integer)), _
   XmlElement("DecimalNumber", GetType(double))> _
   Public MyChoice As Object 

   ' Don't serialize this field. The EnumType field
   ' contains the enumeration value that corresponds
   ' to the MyChoice field value.
   <XmlIgnore> _
   Public EnumType As ItemChoiceType 

   'The ManyChoices field can contain an array
   ' of choices. Each choice must be matched to
   ' an array item in the ChoiceArray field.
   <XmlChoiceIdentifier("ChoiceArray"), _
   XmlElement("Item", GetType(string)), _
   XmlElement("Amount", GetType(Integer)), _
   XmlElement("Temp", GetType(double))> _
   Public ManyChoices() As Object

   ' TheChoiceArray field contains the enumeration
   ' values, one for each item in the ManyChoices array.
   <XmlIgnore> _
   Public ChoiceArray() As MoreChoices
End Class

<XmlType(IncludeInSchema:=false)> _
Public Enum ItemChoiceType
   None
   Word 
   Number
   DecimalNumber
End Enum

<XmlType(IncludeInSchema:=false)> _
Public Enum MoreChoices
   None
   Item
   Amount
   Temp
End Enum

Public Class Test
   Shared Sub Main()
      Dim t  As Test = New Test()
      t.SerializeObject("Choices.xml")
      t.DeserializeObject("Choices.xml")
   End Sub

   private Sub SerializeObject(filename As string)
      Dim mySerializer As XmlSerializer = _
      New XmlSerializer(GetType(Choices))
      Dim writer As TextWriter = New StreamWriter(filename)
      Dim myChoices As Choices = New Choices()

      ' Set the MyChoice field to a string. Set the
      ' EnumType to Word.
      myChoices.MyChoice= "Book"
      myChoices.EnumType = ItemChoiceType.Word

      ' Populate an object array with three items, one
      ' of each enumeration type. Set the array to the 
      ' ManyChoices field.
      Dim strChoices () As Object = New object(){"Food",  5, 98.6}
      myChoices.ManyChoices=strChoices

      ' For each item in the ManyChoices array, add an
      ' enumeration value.
      Dim itmChoices () As MoreChoices = New MoreChoices() _
      {MoreChoices.Item, _
      MoreChoices.Amount, _
      MoreChoices.Temp}
      myChoices.ChoiceArray=itmChoices
      
      mySerializer.Serialize(writer, myChoices)
      writer.Close()
   End Sub

   private Sub DeserializeObject(filename As string)
      Dim ser As XmlSerializer = New XmlSerializer(GetType(Choices))
      

      ' A FileStream is needed to read the XML document.
      Dim fs As FileStream = New FileStream(filename, FileMode.Open)
      
      Dim myChoices As Choices = CType(ser.Deserialize(fs), Choices)

      fs.Close()

      ' Disambiguate the MyChoice value Imports the enumeration.
      if myChoices.EnumType = ItemChoiceType.Word Then
           Console.WriteLine("Word: " & _
           myChoices.MyChoice.ToString())
        
      else if myChoices.EnumType = ItemChoiceType.Number Then 
           Console.WriteLine("Number: " & _
            myChoices.MyChoice.ToString())
        
      else if myChoices.EnumType = ItemChoiceType.DecimalNumber Then
         Console.WriteLine("DecimalNumber: " & _
            myChoices.MyChoice.ToString())
        End If

      ' Disambiguate the ManyChoices values Imports the enumerations.
      Dim i As Integer
      for i = 0 to myChoices.ManyChoices.Length -1
      if myChoices.ChoiceArray(i) = MoreChoices.Item Then
        Console.WriteLine("Item: " +  _
        myChoices.ManyChoices(i).ToString())
      else if myChoices.ChoiceArray(i) = MoreChoices.Amount Then
        Console.WriteLine("Amount: " + _
        myChoices.ManyChoices(i).ToString())
      else if (myChoices.ChoiceArray(i) = MoreChoices.Temp)
        Console.WriteLine("Temp: " + _
        myChoices.ManyChoices(i).ToString())
        End If
        Next i
      
   End Sub
End Class
using System;
using System.Xml;
using System.Xml.Serialization;
using System.IO;

public class Choices{
   // The MyChoice field can be set to any one of 
   // the types below. 
   [XmlChoiceIdentifier("EnumType")]
   [XmlElement("Word", typeof(string))]
   [XmlElement("Number", typeof(int))]
   [XmlElement("DecimalNumber", typeof(double))]
   public object MyChoice;

   // Don't serialize this field. The EnumType field
   // contains the enumeration value that corresponds
   // to the MyChoice field value.
   [XmlIgnore]
   public ItemChoiceType EnumType;

   // The ManyChoices field can contain an array
   // of choices. Each choice must be matched to
   // an array item in the ChoiceArray field.
   [XmlChoiceIdentifier("ChoiceArray")]
   [XmlElement("Item", typeof(string))]
   [XmlElement("Amount", typeof(int))]
   [XmlElement("Temp", typeof(double))]
   public object[] ManyChoices;

   // TheChoiceArray field contains the enumeration
   // values, one for each item in the ManyChoices array.
   [XmlIgnore]
   public MoreChoices[] ChoiceArray;
}

[XmlType(IncludeInSchema=false)]
public enum ItemChoiceType{
   None,
   Word, 
   Number,
   DecimalNumber
}

public enum MoreChoices{
   None,
   Item,
   Amount,
   Temp
}

public class Test{
   static void Main(){
      Test t = new Test();
      t.SerializeObject("Choices.xml");
      t.DeserializeObject("Choices.xml");
   }

   private void SerializeObject(string filename){
      XmlSerializer mySerializer = 
      new XmlSerializer(typeof(Choices));
      TextWriter writer = new StreamWriter(filename);
      Choices myChoices = new Choices();

      // Set the MyChoice field to a string. Set the
      // EnumType to Word.
      myChoices.MyChoice= "Book";
      myChoices.EnumType = ItemChoiceType.Word;

      // Populate an object array with three items, one
      // of each enumeration type. Set the array to the 
      // ManyChoices field.
      object[] strChoices = new object[]{"Food",  5, 98.6};
      myChoices.ManyChoices=strChoices;

      // For each item in the ManyChoices array, add an
      // enumeration value.
      MoreChoices[] itmChoices = new MoreChoices[]
      {MoreChoices.Item, 
      MoreChoices.Amount,
      MoreChoices.Temp};
      myChoices.ChoiceArray=itmChoices;
      
      mySerializer.Serialize(writer, myChoices);
      writer.Close();
   }

   private void DeserializeObject(string filename){
      XmlSerializer ser = new XmlSerializer(typeof(Choices));

      // A FileStream is needed to read the XML document.
      FileStream fs = new FileStream(filename, FileMode.Open);
      Choices myChoices = (Choices)
      ser.Deserialize(fs);
      fs.Close();

      // Disambiguate the MyChoice value using the enumeration.
      if(myChoices.EnumType == ItemChoiceType.Word){
           Console.WriteLine("Word: " +  
            myChoices.MyChoice.ToString());
        }
      else if(myChoices.EnumType == ItemChoiceType.Number){
           Console.WriteLine("Number: " +
            myChoices.MyChoice.ToString());
        }
      else if(myChoices.EnumType == ItemChoiceType.DecimalNumber){
           Console.WriteLine("DecimalNumber: " +
            myChoices.MyChoice.ToString());
        }

      // Disambiguate the ManyChoices values using the enumerations.
      for(int i = 0; i<myChoices.ManyChoices.Length; i++){
      if(myChoices.ChoiceArray[i] == MoreChoices.Item)
        Console.WriteLine("Item: " + (string) myChoices.ManyChoices[i]);
      else if(myChoices.ChoiceArray[i] == MoreChoices.Amount)
        Console.WriteLine("Amount: " + myChoices.ManyChoices[i].ToString());
      if(myChoices.ChoiceArray[i] == MoreChoices.Temp)
        Console.WriteLine("Temp: " + (string) myChoices.ManyChoices[i].ToString());
        }
      
   }
}
#using <System.dll>
#using <System.xml.dll>

using namespace System;
using namespace System::Xml;
using namespace System::Xml::Serialization;
using namespace System::IO;

[XmlType(IncludeInSchema=false)]

public enum class ItemChoiceType
{
   None, Word, Number, DecimalNumber
};

public enum class MoreChoices
{
   None, Item, Amount, Temp
};

public ref class Choices
{
public:

   // The MyChoice field can be set to any one of 
   // the types below. 

   [XmlChoiceIdentifier("EnumType")]
   [XmlElement("Word",String::typeid)]
   [XmlElement("Number",Int32::typeid)]
   [XmlElement("DecimalNumber",Double::typeid)]
   Object^ MyChoice;

   // Don't serialize this field. The EnumType field
   // contains the enumeration value that corresponds
   // to the MyChoice field value.

   [XmlIgnore]
   ItemChoiceType EnumType;

   // The ManyChoices field can contain an array
   // of choices. Each choice must be matched to
   // an array item in the ChoiceArray field.

   [XmlChoiceIdentifier("ChoiceArray")]
   [XmlElement("Item",String::typeid)]
   [XmlElement("Amount",Int32::typeid)]
   [XmlElement("Temp",Double::typeid)]
   array<Object^>^ManyChoices;

   // TheChoiceArray field contains the enumeration
   // values, one for each item in the ManyChoices array.

   [XmlIgnore]
   array<MoreChoices>^ChoiceArray;
};

void SerializeObject( String^ filename );
void DeserializeObject( String^ filename );
int main()
{
   SerializeObject( "Choices.xml" );
   DeserializeObject( "Choices.xml" );
}

void SerializeObject( String^ filename )
{
   XmlSerializer^ mySerializer = gcnew XmlSerializer( Choices::typeid );
   TextWriter^ writer = gcnew StreamWriter( filename );
   Choices^ myChoices = gcnew Choices;

   // Set the MyChoice field to a string. Set the
   // EnumType to Word.
   myChoices->MyChoice = "Book";
   myChoices->EnumType = ItemChoiceType::Word;

   // Populate an object array with three items, one
   // of each enumeration type. Set the array to the 
   // ManyChoices field.
   array<Object^>^strChoices = {"Food",5,98.6};
   myChoices->ManyChoices = strChoices;

   // For each item in the ManyChoices array, add an
   // enumeration value.
   array<MoreChoices>^ itmChoices = {MoreChoices::Item,MoreChoices::Amount,MoreChoices::Temp};
   myChoices->ChoiceArray = itmChoices;
   mySerializer->Serialize( writer, myChoices );
   writer->Close();
}

void DeserializeObject( String^ filename )
{
   XmlSerializer^ ser = gcnew XmlSerializer( Choices::typeid );

   // A FileStream is needed to read the XML document.
   FileStream^ fs = gcnew FileStream( filename,FileMode::Open );
   Choices^ myChoices = safe_cast<Choices^>(ser->Deserialize( fs ));
   fs->Close();

   // Disambiguate the MyChoice value using the enumeration.
   if ( myChoices->EnumType == ItemChoiceType::Word )
   {
      Console::WriteLine( "Word: {0}", myChoices->MyChoice->ToString() );
   }
   else
   if ( myChoices->EnumType == ItemChoiceType::Number )
   {
      Console::WriteLine( "Number: {0}", myChoices->MyChoice->ToString() );
   }
   else
   if ( myChoices->EnumType == ItemChoiceType::DecimalNumber )
   {
      Console::WriteLine( "DecimalNumber: {0}", myChoices->MyChoice->ToString() );
   }

   // Disambiguate the ManyChoices values using the enumerations.
   for ( int i = 0; i < myChoices->ManyChoices->Length; i++ )
   {
      if ( myChoices->ChoiceArray[ i ] == MoreChoices::Item )
            Console::WriteLine( "Item: {0}", myChoices->ManyChoices[ i ] );
      else
      if ( myChoices->ChoiceArray[ i ] == MoreChoices::Amount )
            Console::WriteLine( "Amount: ", myChoices->ManyChoices[ i ]->ToString() );
      if ( myChoices->ChoiceArray[ i ] == MoreChoices::Temp )
            Console::WriteLine( "Temp: {0}", myChoices->ManyChoices[ i ]->ToString() );
   }
}
import System.*;
import System.Xml.*;
import System.Xml.Serialization.*;
import System.IO.*;
public class Choices
{
    // The myChoice field can be set to any one of 
    // the types below. 
    /** @attribute XmlElement("word", String.class)
     */
    /** @attribute XmlElement("Number", int.class)
     */
    /** @attribute XmlElement("DecimalNumber", double.class)
     */
    public Object myChoice;

    // Don't serialize this field. The EnumType field
    // contains the enumeration value that corresponds
    // to the myChoice field value.
    public int enumType;

    // The manyChoices field can contain an array
    // of choices. Each choice must be matched to
    // an array item in the choiceArray field.
    /** @attribute XmlElement("Item", String.class)
     */
    /** @attribute XmlElement("amount", int.class)
     */
    /** @attribute XmlElement("temp", double.class)
     */
    public Object manyChoices[];

    // ThechoiceArray field contains the enumeration
    // values, one for each item in the manyChoices array.
    public int choiceArray[];
} //Choices

/** @attribute XmlType(IncludeInSchema = false)
 */
public class ItemChoiceType
{
    public static final int none = 0;
    public static final int word = 1;
    public static final int number = 2;
    public static final int decimalNumber = 3;
} //ItemChoiceType

public class MoreChoices
{
    public static final int none = 0;
    public static final int item = 1;
    public static final int amount = 2;
    public static final int temp = 3;
} //MoreChoices

public class Test
{
    public static void main(String[] args)
    {
        Test t = new Test();
        t.SerializeObject("Choices.xml");
        t.DeserializeObject("Choices.xml");
    } //main

    private void SerializeObject(String fileName)
    {
        XmlSerializer mySerializer = new XmlSerializer(Choices.class.ToType());
        TextWriter writer = new StreamWriter(fileName);
        Choices myChoices = new Choices();
        // Set the myChoice field to a string. Set the
        // enumType to word.
        myChoices.myChoice = "Book";
        myChoices.enumType = ItemChoiceType.word;
        // Populate an object array with three items, one
        // of each enumeration type. Set the array to the 
        // manyChoices field.
        Object strChoices[] = new Object[] { "Food", (Int32)5,
            (System.Double)98.6 };
        myChoices.manyChoices = strChoices;
        // For each item in the manyChoices array, add an
        // enumeration value.
        int itmChoices[] = new int[] {
            MoreChoices.item, MoreChoices.amount, MoreChoices.temp
        };

        myChoices.choiceArray = new int[itmChoices.get_Length()];
        itmChoices.CopyTo(myChoices.choiceArray, 0);

        //myChoices.choiceArray = itmChoices;
        mySerializer.Serialize(writer, myChoices);
        writer.Close();
    } //SerializeObject

    private void DeserializeObject(String fileName)
    {
        XmlSerializer ser = new XmlSerializer(Choices.class.ToType());
        // A FileStream is needed to read the XML document.
        FileStream fs = new FileStream(fileName, FileMode.Open);
        Choices myChoices = (Choices)ser.Deserialize(fs);
        fs.Close();
        // Disambiguate the myChoice value using the enumeration.
        if (myChoices.enumType == ItemChoiceType.word) {
            Console.WriteLine("Word: " + myChoices.myChoice.ToString());
        }
        else {
            if (myChoices.enumType == ItemChoiceType.number) {
                Console.WriteLine("Number: " + myChoices.myChoice.ToString());
            }
            else {
                if (myChoices.enumType == ItemChoiceType.decimalNumber) {
                    Console.WriteLine("DecimalNumber: " +
                        myChoices.myChoice.ToString());
                }
            }
        }
        // Disambiguate the manyChoices values using the enumerations.
        for (int i = 0; i < myChoices.manyChoices.get_Length(); i++) {
            if (System.Convert.ToInt32(myChoices.choiceArray.get_Item(i)) ==
                MoreChoices.item) {
                Console.WriteLine("Item: " 
                    + (String)(myChoices.manyChoices.get_Item(i)));
            }
            else {
                if (System.Convert.ToInt32(myChoices.choiceArray.
                    get_Item(i)) == MoreChoices.amount) {
                    Console.WriteLine("Amount: " +
                        myChoices.manyChoices.get_Item(i).ToString());
                }
            }
            if (System.Convert.ToInt32(myChoices.choiceArray.
                get_Item(i)) == MoreChoices.temp) {
                Console.WriteLine("Temp: " + (String)(
                    myChoices.manyChoices.get_Item(i).ToString()));
            }
        }
    } //DeserializeObject 
} //Test

Plattformen

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile für Pocket PC, Windows Mobile für Smartphone, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

.NET Framework unterstützt nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen.

Versionsinformationen

.NET Framework

Unterstützt in: 2.0, 1.1, 1.0

.NET Compact Framework

Unterstützt in: 2.0, 1.0

Siehe auch

Referenz

XmlChoiceIdentifierAttribute-Klasse
XmlChoiceIdentifierAttribute-Member
System.Xml.Serialization-Namespace