Export (0) Print
Expand All

XmlChoiceIdentifierAttribute.MemberName Property

Gets or sets the name of the field that returns the enumeration to use when detecting types.

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

public string MemberName { get; set; }

Property Value

Type: System.String
The name of a field that returns an enumeration.

At least one member must be present in the enumeration returned by the field named in the MemberName value. By default, that enumeration name takes the name of the field that the XmlChoiceIdentifierAttribute is applied to.

The following example serializes a class named Choices that includes two fields, MyChoice and ManyChoices. The XmlChoiceIdentifierAttribute is applied to each field that specifies (through the MemberName property) another class member that gets or sets an enumeration that detects the member value. The MyChoice field can be set to a single value, with a corresponding enumeration member found in the EnumType field. The ManyChoices field returns an array of objects. The ChoiceArray field returns an array of enumeration values. For each array member in the ManyChoices field, a corresponding member is found in the array returned by the ChoiceArray field.

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 <mscorlib.dll>
#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 __value enum ItemChoiceType
{
   None,
   Word, 
   Number,
   DecimalNumber
};

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

public __gc class Choices
{
public:
   // The MyChoice field can be set to any one of 
   // the types below. 
   [XmlChoiceIdentifier("EnumType")]
   [XmlElement("Word", __typeof(String))]
   [XmlElement("Number", __typeof(Int32))]
   [XmlElement("DecimalNumber", __typeof(Double))]
   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", __typeof(String))]
   [XmlElement("Amount", __typeof(Int32))]
   [XmlElement("Temp", __typeof(Double))]
   Object*	ManyChoices[];

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

void SerializeObject(String* filename);
void DeserializeObject(String* filename);

int main(){
   SerializeObject(S"Choices.xml");
   DeserializeObject(S"Choices.xml");
}

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= S"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[] = {S"Food", __box(5), __box(98.6) };
   myChoices->ManyChoices = strChoices;

   // For each item in the ManyChoices array, add an
   // enumeration value.
   MoreChoices	itmChoices[] =
   {MoreChoices::Item, 
   MoreChoices::Amount,
   MoreChoices::Temp};
   myChoices->ChoiceArray = itmChoices;

   mySerializer->Serialize(writer, myChoices);
   writer->Close();
}

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 = __try_cast<Choices*>( ser->Deserialize(fs));
   fs->Close();

   // Disambiguate the MyChoice value using the enumeration.
   if(myChoices->EnumType == ItemChoiceType::Word)
   {
      Console::WriteLine( S"Word: {0}", myChoices->MyChoice->ToString());
   }
   else if( myChoices->EnumType == ItemChoiceType::Number)
   {
      Console::WriteLine( S"Number: {0}", myChoices->MyChoice->ToString());
   }
   else if( myChoices->EnumType == ItemChoiceType::DecimalNumber)
   {
      Console::WriteLine( S"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( S"Item: {0}", myChoices->ManyChoices[i]);	
      else if ( myChoices->ChoiceArray[i] == MoreChoices::Amount )
         Console::WriteLine( S"Amount: ", myChoices->ManyChoices[i]->ToString());
      if(myChoices->ChoiceArray[i] == MoreChoices::Temp)
         Console::WriteLine(S"Temp: {0}" , myChoices->ManyChoices[i]->ToString());
   }

}

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0

.NET Compact Framework

Supported in: 3.5, 2.0, 1.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Community Additions

ADD
Show:
© 2014 Microsoft