在C#中进行序列化时,如何为数组赋予属性?

在C#中进行序列化时,如何为数组赋予属性?,c#,xml-serialization,xml-attribute,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> <

我正在尝试生成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>
作为属性,使用此类声明:

public class ListItem {
    [XmlAttribute]
    public string type { get; set; }

    [XmlText]
    public string Value { get; set; }
}
这提供了内部序列化,但我不知道如何将
type=“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; }
}
和类型,同时在代码中声明如下:

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>

1234566
1234566

因此,显然我仍然缺少一些东西…

不是使用
列表项[]
,而是从
列表
派生一个名为DeviceList的新类:

public class DeviceList : List<ListItem>
{
   [XmlElement(ElementName = "type")]
   public string ListType {get;set;}

}

您还可以通过在容器类中实现
[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>

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<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);
        }
    }
}
公共类项目
{
[XmlAttribute(“类型”)]
公共字符串类型{get;set;}
[XmlText]
公共字符串值{get;set;}
}
公共类设备列表:IXmlSerializable
{
公共字符串类型{get;set;}
公共列表项{get;set;}
public System.Xml.Schema.XmlSchema GetSchema()
{
返回null;
}
public void ReadXml(System.Xml.XmlReader)
{
reader.MoveToContent();
}
public void WriteXml(System.Xml.XmlWriter)
{
WriteAttributeString(“type”,type);
XmlSerializer serializer=新的XmlSerializer(typeof(Item));
foreach(项目中的var项目)
{
序列化器。序列化(编写器,项);
}
}
}
我在主要方法中使用以下代码:

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);
}
var-dlist=新设备列表
{
Type=“list”,
项目=新列表
{
新项{Type=“MAC”,Value=“11:22:33:44:55:66:77:88”},
新项{Type=“MAC”,Value=“11:22:33:44:55:66:77:89”},
新项{Type=“MAC”,Value=“11:22:33:44:55:66:77:8A”},
}
};
使用(FileStream stream=newfilestream(@“D:\jcoletest.xml”,FileMode.Create,FileAccess.Write))
{
新的XmlSerializer(typeof(DeviceList)).Serialize(stream,dlist);
}

有关更多信息,请使用上面Dave的部分答案查看此

,我发现最好在声明类中使用如下属性:(注意缺少属性)

然后更新DeviceList类,如下所示:

[XmlArray( "device_list" ), XmlArrayItem("item")]
public ListItem[] device_list { get; set; }
[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>

1234567
123456890

那么我在哪里使用
[XmlArray(“设备列表”),XmlArrayItem(“项目”)]public ListItem[]设备列表{get;set;}
你建议我应该做什么?很抱歉太密集了,我只是觉得这里缺少了一些东西…ListItem[]是ListItems的原始数组。若您创建一个从列表派生的新类,则可以向该类添加一个新的“type”属性。这将使您能够在序列化的XML中设置“type”元素。更新了Q,这样您就可以看到我正在做什么,以及哪里没有看到序列化。我做错了什么?事实上,不,那也没用。。但是我找到了一个solution@jcolebrand-您需要向DeviceList类的ListType属性添加XmlElement属性。请参阅下面的更新内容。@jcolebrand-这里有一些关于XML序列化的好参考:我花了一段时间才发现添加[XmlElement]属性是这里的关键。根据MSDN:如果将XmlElementAttribute应用于返回数组的字段或属性,那么数组中的项将被编码为XML元素序列。相反,如果XmlElementAttribute未应用于此类字段或属性,则数组中的项将被编码为元素序列,嵌套在以字段或属性命名的元素下。这非常有效。如果您有多个具有相同父类型的子类型,那么您甚至可以应用多个XmlElement引用。
[XmlType("device_list")]
[Serializable]
public class DeviceList {
    [XmlAttribute]
    public string type { get; set; }

    [XmlElement( "item" )]
    public ListItem[] items { get; set; }
}
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>