2017-08-16 4 views
-1

XMLファイルにシリアル化するこのクラスがあります。以下のように、クラスの各プロパティに "Description"属性を追加します。出来ますか?または私はこれをどのように達成することができますか?事前にデザイン時にクラスの各XMLElementに「説明」を定義する方法

<ID description="It defines ID number of the Arm">1</ID> 
<Dimension description="It defines the dimension of the Arm"> 
    <XMin>-150</XMin> 
    <XMax>150</XMax> 
    <YMin>-300</YMin> 
    <YMax>300</YMax> 
</Dimension> 

ありがとう:私は、次の結果を持っていると思います

[Serializable] 
public class Arm : INotifyPropertyChanged{ 

    private int _ID; 
    private ArmStore _aStore; 
    private ArmDimension _dimension; 
    private Zone _accessibleZone; 

    [XmlElement("ID")] 
    [XmlAttribute("description"), Value="It defines ID number of the Arm"] 
    public int ID { 
     get { return _ID; } 
     set { _ID = value; } 
    } 

    [XmlElement("Store")] 
    [XmlAttribute("description"), Value="It defines the Store of the Arm"] 
    public ArmStore aStore { 
     get { return _aStore; } 
     set { 
      _aStore = value; 
      Notify("aStore"); 
     } 
    } 

    [XmlElement("Dimension")] 
    [XmlAttribute("description"), Value="It defines the dimension of the Arm"] 
    public ArmDimension dimension { 
     get { return _dimension; } 
     set { 
      _dimension = value; 
      Notify("dimension"); 
     } 
    } 

答えて

1

あなたが作成したカスタム所望の特性

public class Arm 
{ 
    [XmlElement("ID")] 
    [XmlDescription(Value = "It defines ID number of the Arm")] 
    public int ID { get; set; } 

    [XmlElement("Store")] 
    [XmlDescription(Value = "It defines the Store of the Arm")] 
    public ArmStore Store { get; set; } 

    [XmlElement("Dimension")] 
    [XmlDescription(Value = "It defines the dimension of the Arm")] 
    public ArmDimension Dimension { get; set; } 
} 

次へ

[AttributeUsage(AttributeTargets.Property)] 
public class XmlDescription : Attribute 
{ 
    public string Value { get; set; } 
} 

属性と、それを設定することができ、カスタムのXmlWriterを作成する必要が

public class DescriptionWriter : XmlTextWriter 
{ 
    public DescriptionWriter(string filename, Encoding encoding) : base(filename, encoding) { } 

    public override void WriteStartElement(string prefix, string localName, string ns) 
    { 
     base.WriteStartElement(prefix, localName, ns); 

     var prop = typeof(Arm).GetProperty(localName); 
     if (prop != null) 
     { 
      var data = prop.GetCustomAttributesData(); 
      var description = data.FirstOrDefault(a => a.AttributeType == typeof(XmlDescription)); 
      if (description != null) 
      { 
       var value = description.NamedArguments.First().TypedValue.ToString().Trim('"'); 
       base.WriteAttributeString("description", value); 
      } 
     } 
    } 
} 

この実装には多くの欠点があります。特に、プロパティ名とXmlElement名は同じでなければなりません。または、名前でプロパティを取得することはできません:GetProperty(localName)

Arm arm = ... 

var xs = new XmlSerializer(typeof(Arm)); 

using (var writer = new DescriptionWriter("test.xml", Encoding.Unicode)) 
{ 
    writer.Formatting = Formatting.Indented; 
    xs.Serialize(writer, arm); 
} 
を次のようにそれを使用します
0

試してみてください。

[XmlRoot("Arm")] 
    public class Arm 
    { 
     [XmlElement("ID")] 
     public ID id {get;set;} 

     [XmlElement("Dimension")] 
     public Dimension dimension { get; set;} 
    } 
    [XmlRoot("Dimension")] 
    public class Dimension 
    { 
     [XmlAttribute("description")] 
     public string Value { get; set; } 

     [XmlElement("XMin")] 
     public int XMin { get; set; } 

     [XmlElement("XMax")] 
     public int XMax { get; set; } 

     [XmlElement("YMin")] 
     public int YMin { get; set; } 

     [XmlElement("YMax")] 
     public int YMax { get; set; } 
    } 

    [XmlElement("ID")] 
    public class ID 
    { 
     [XmlAttribute("description")] 
     public string Value { get; set; } 

     [XmlText] 
     public int value { get; set; } 
    } 
関連する問題