如何在C#中序列化期间为数组赋予属性?

时间:2012-01-09 15:30:29

标签: c# xml-serialization xml-attribute

我正在尝试生成C#,它会像这样创建一个XML片段。

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

显然我仍然缺少一些东西......

3 个答案:

答案 0 :(得分:12)

使用上面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; }
}

我的序列化符合预期:

<device_list type="list">
  <item type="MAC">1234567</item>
  <item type="MAC">123456890</item>
</device_list>

答案 1 :(得分:4)

不使用ListItem[],而是从List<T>派生一个名为DeviceList的新类:

public class DeviceList : List<ListItem>
{
   [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;}
}

答案 2 :(得分:3)

您还可以通过在容器类中实现[IXmlSerializable][1]来实现所需的行为:

使用以下代码,我得到以下标记:

<?xml version="1.0"?>
<DeviceList type="list">
  <Item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" type="MAC">11:22:33:44:55:66:77:88</Item>
  <Item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" type="MAC">11:22:33:44:55:66:77:89</Item>
  <Item xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" type="MAC">11:22:33:44:55:66:77:8A</Item>
</DeviceList>

代码:

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<Item> 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<Item>
                                {
                                    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);
}

有关详细信息,请查看此tutorial here