如何在设计时为 class 的每个 XMLElement 定义 "description"

How to define a "description" for each XMLElement of a class at design time

我有这个 class,我想将其序列化为 XML 文件。我想向 class 的每个 属性 添加一个 "Description" 属性,如下所示。可能吗?或者我怎样才能做到这一点?

 [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");
        }
    }

我想要得到以下结果:

<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>

提前致谢!

尝试以下操作:

    [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; }
    }

您可以创建自定义属性

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

并将其设置为所需的属性

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; }
}

接下来,您需要创建一个自定义的 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);
}