このような XML のフラグメントを作成する C# を生成しようとしています。
<device_list type="list">
<item type="MAC">11:22:33:44:55:66:77:88</item>
<item type="MAC">11:22:33:44:55:66:77:89</item>
<item type="MAC">11:22:33:44:55:66:77:8A</item>
</device_list>
私はこのようなものを使用することを考えていました:
[XmlArray( "device_list" ), XmlArrayItem("item")]
public ListItem[] device_list { get; set; }
プロパティとして、次のクラス宣言を使用します。
public class ListItem {
[XmlAttribute]
public string type { get; set; }
[XmlText]
public string Value { get; set; }
}
type="list"
これにより、内部のシリアル化が得られますが、属性を上記に適用する方法がわかりませんdevice_list
。
私は次のことをする必要があると考えています(ただし、構文の書き方はわかりません):
public class DeviceList {
[XmlAttribute]
public string type { get; set; }
[XmlArray]
public ListItem[] .... This is where I get lost
}
Dave の回答に基づいて更新
public class DeviceList : List<ListItem> {
[XmlAttribute]
public string type { get; set; }
}
public class ListItem {
[XmlAttribute]
public string type { get; set; }
[XmlText]
public string Value { get; set; }
}
現在の使用法は次のとおりです。
[XmlArray( "device_list" ), XmlArrayItem("item")]
public DeviceList device_list { get; set; }
そして、型は、次のようにコードで宣言されています。
device_list = new DeviceList{type = "list"}
device_list.Add( new ListItem { type = "MAC", Value = "1234566" } );
device_list.Add( new ListItem { type = "MAC", Value = "1234566" } );
シリアル化の型を表示していません。シリアル化の結果は次のとおりです。
<device_list>
<item type="MAC">1234566</item>
<item type="MAC">1234566</item>
</device_list>
どうやら私はまだ何かが欠けているようです...