如何在C#中序列化期间为数组赋予属性?
我正在尝试生成像这样创建XML片段的C#。
11:22:33:44:55:66:77:88 11:22:33:44:55:66:77:89 11:22:33:44:55:66:77:8A
我在考虑使用这样的东西:
[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 { [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" } );
没有在序列化上显示类型。 这是序列化的结果:
1234566 1234566
显然我仍然遗漏了一些东西……
使用上面Dave的部分答案,我发现最好在声明类中使用这个属性:(注意缺少属性)
public DeviceList device_list { get; set; }
然后像这样更新DeviceList类:
[XmlType("device_list")] [Serializable] public class DeviceList { [XmlAttribute] public string type { get; set; } [XmlElement( "item" )] public ListItem[] items { get; set; } }
并保留原始的ListItem类
public class ListItem { [XmlAttribute] public string type { get; set; } [XmlText] public string Value { get; set; } }
我的序列化是按预期的:
- 1234567
- 123456890
而不是使用ListItem[]
,从List
派生一个名为DeviceList的新类:
public class DeviceList : List { [XmlElement(ElementName = "type")] public string ListType {get;set;} }
然后,在包含类中使用该类来序列化XML。 类型值可以作为父节点的元素包含在内,具体取决于配置序列化的方式。 我不记得确切的语法,但我认为类属性默认添加为节点元素。
包含类:
public class SerializeMyStuff { public SeriazlieMyStuff() { ListOfDevices = new DeviceList(); ListOfDevices.ListType = "list"; } [XmlArray( "device_list" ), XmlArrayItem("item")] public DeviceList ListOfDevices {get;set;} }
您还可以通过在容器类中实现[IXmlSerializable][1]
来实现所需的行为:
使用下面的代码我得到以下标记:
- 11:22:33:44:55:66:77:88
- 11:22:33:44:55:66:77:89
- 11:22:33:44:55:66:77:8A
码:
public class Item { [XmlAttribute("type")] public string Type { get; set; } [XmlText] public string Value { get; set; } } public class DeviceList : IXmlSerializable { public string Type { get; set; } public List- Items { get; set; } public System.Xml.Schema.XmlSchema GetSchema() { return null; } public void ReadXml(System.Xml.XmlReader reader) { reader.MoveToContent(); } public void WriteXml(System.Xml.XmlWriter writer) { writer.WriteAttributeString("type", Type); XmlSerializer serializer = new XmlSerializer(typeof(Item)); foreach (var item in Items) { serializer.Serialize(writer, item); } } }
我在main方法中使用以下代码:
var dlist = new DeviceList { Type = "list", Items = new List- { new Item {Type = "MAC", Value = "11:22:33:44:55:66:77:88"}, new Item {Type = "MAC", Value = "11:22:33:44:55:66:77:89"}, new Item {Type = "MAC", Value = "11:22:33:44:55:66:77:8A"}, } }; using(FileStream stream = new FileStream(@"D:\jcoletest.xml", FileMode.Create, FileAccess.Write)) { new XmlSerializer(typeof (DeviceList)).Serialize(stream, dlist); }
有关更多信息,请在此处查看本教程