2

ID と値、および名前を持つプロパティがあります。XmlElement/XmlArray C# 注釈を使用して、これらすべてを 1 つのクラスで表すことはできますか? クラス属性名からxml要素名を導出したいと思います。

私のクラスは次のようになります。

public class Property {
   public string name; //could be enum
   public int id; 
   public string value;
}

例えば:

new Property("property1name",2,"testvalue");
new Property("property2name",10,"anothervalue");

次のようなxmlが必要です。

<property1name><id>2</id><value>testvalue</value></property1name>
<property2name><id>10</id><value>anothervalue</value></property2name>

いつもの代わりに

<property><name>property1name</name><id>2</id><value>testvalue</value></property>
<property><name>property2name</name><id>10</id><value>anothervalue</value></property>

言い換えれば、xmlelement はクラス Property の属性名からその名前を取得します。

4

2 に答える 2

1

UnitItem が次のように変更された場合

  public class UnitItem
    {
        public string AAA;
        public string BBB;
    }

XMLは次のようになります

<Items>
   <UnitItem>
         <AAA>testa</AAA>
         <BBB>testb</BBB>
   </UnitItem>
</Items>
于 2016-01-04T14:18:52.810 に答える