XmlAnyElementAttribute.Name プロパティ

定義

XML 要素名を取得または設定します。

C#
public string Name { get; set; }

プロパティ値

String

XML 要素の名前。

例外

配列メンバーの要素名が、Name プロパティに指定されている要素名と一致しません。

C#
using System;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using System.Xml;
using System.Xml.Schema;

[XmlRoot(Namespace = "http://www.cohowinery.com")]
public class Group{
   public string GroupName;

   // This is for serializing Employee elements.
   [XmlAnyElement(Name = "Employee")]
   public XmlElement[] UnknownEmployees;

   // This is for serializing City elements.   
   [XmlAnyElement
   (Name = "City", 
   Namespace = "http://www.cpandl.com")]
   public XmlElement[] UnknownCity;

    // This one is for all other unknown elements.
   [XmlAnyElement]
   public XmlElement[] UnknownElements;
}

public class Test{
   static void Main(){
      Test t = new Test();
      t.SerializeObject("AnyElementArray.xml");
      t.DeserializeObject("AnyElementArray.xml");
      Console.WriteLine("Done");
   }

   private void SerializeObject(string filename){
      XmlSerializer ser = new XmlSerializer(typeof(Group));
      // Create an XmlNamespaces to use.
      XmlSerializerNamespaces namespaces =
      new XmlSerializerNamespaces();
      namespaces.Add("c", "http://www.cohowinery.com");
      namespaces.Add("i", "http://www.cpandl.com");
      Group myGroup = new Group();
      // Create arrays of arbitrary XmlElement objects.
      // First create an XmlDocument, used to create the 
      // XmlElement objects.
      XmlDocument xDoc = new XmlDocument();

      // Create an array of Employee XmlElement objects.
      XmlElement El1 = xDoc.CreateElement("Employee", "http://www.cohowinery.com");
      El1.InnerText = "John";
      XmlElement El2 = xDoc.CreateElement("Employee", "http://www.cohowinery.com");
      El2.InnerText = "Joan";
      XmlElement El3 = xDoc.CreateElement("Employee", "http://www.cohowinery.com");
      El3.InnerText = "Jim";
      myGroup.UnknownEmployees= new XmlElement[]{El1, El2, El3};     
    
      // Create an array of City XmlElement objects.
      XmlElement inf1 = xDoc.CreateElement("City", "http://www.cpandl.com");
      inf1.InnerText = "Tokyo";
      XmlElement inf2 = xDoc.CreateElement("City", "http://www.cpandl.com");     
      inf2.InnerText = "New York";
      XmlElement inf3 = xDoc.CreateElement("City", "http://www.cpandl.com");     
      inf3.InnerText = "Rome";

      myGroup.UnknownCity = new XmlElement[]{inf1, inf2, inf3};

      XmlElement xEl1 = xDoc.CreateElement("bld");
      xEl1.InnerText = "42";
      XmlElement xEl2 = xDoc.CreateElement("Region");
      xEl2.InnerText = "West";
      XmlElement xEl3 = xDoc.CreateElement("type");
      xEl3.InnerText = "Technical";
      myGroup.UnknownElements = 
        new XmlElement[]{xEl1,xEl2,xEl3};
      // Serialize the class, and close the TextWriter.
      TextWriter writer = new StreamWriter(filename);
      ser.Serialize(writer, myGroup, namespaces);
      writer.Close();
   }

   private void DeserializeObject(string filename){
      XmlSerializer ser = new XmlSerializer(typeof(Group));
      FileStream fs = new FileStream(filename, FileMode.Open);
      Group myGroup;
      myGroup = (Group)ser.Deserialize(fs);
      fs.Close();
      foreach(XmlElement xEmp in myGroup.UnknownEmployees){
         Console.WriteLine(xEmp.LocalName + ": " + xEmp.InnerText);}
      foreach(XmlElement xCity in myGroup.UnknownCity){
         Console.WriteLine(xCity.LocalName + ": " + xCity.InnerText);}
      foreach(XmlElement xEl in myGroup.UnknownElements){
         Console.WriteLine(xEl.LocalName + ": " + xEl.InnerText);}
   }
 }

注釈

属性の適用時に Name プロパティ値を指定する場合、配列に挿入されるすべての XmlElement オブジェクトまたは XmlNode オブジェクトは、同じ要素名と既定の名前空間を持っているか、例外がスローされる必要があります。 プロパティ値を設定する Namespace 場合は、プロパティも設定する Name 必要があります XmlElement 。また、オブジェクトの XmlNode 名前と名前空間の値も同じである必要があります。 値が指定されていないName場合、オブジェクトにはXmlNode任意のXmlElement要素名を指定できます。

クラスのXmlSerializerメソッドをDeserialize呼び出すと、逆シリアル化されるオブジェクトに対応するメンバーを持たないすべての属性が配列に収集されます。 値を Name 指定した場合、配列にはその名前の XML 要素のみが含まれます。 値を Name 指定しない場合、配列には、クラスに対応するメンバーを持たないすべての要素が含まれます。 属性が適用される複数のフィールドがクラスに含まれている場合は、プロパティとNamespaceプロパティをName使用して配列の内容を区別します。 このようなクラス (複数のフィールドを含む) にも、逆シリアル化中に区別プロパティ値が設定されていないフィールド (つまり、 Name Namespace逆シリアル化中) が含まれている場合、配列には他の配列にまだ含まれていない XML 要素が含まれます。 区別またはNamespace値が設定されていない複数のフィールドをName追加する場合、クラスの最後のフィールドには、他の配列にまだ含まれていない不明な要素がすべて含まれており、その他のフィールドはすべてに設定nullされます。

クラス メンバーには複数のインスタンスを XmlAnyElementAttribute 適用できますが、各インスタンスには個別 Name のプロパティ値が必要です。 または、インスタンスごとに同じ Name プロパティが設定されている場合は、インスタンスごとに個別 Namespace のプロパティ値を設定する必要があります。

適用対象

製品 バージョン
.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