我如何给一个数组的属性在C#中序列化过程中? [英] How do I give an array an attribute during serialization in C#?

查看:181
本文介绍了我如何给一个数组的属性在C#中序列化过程中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图生成一个用于创建XML这样的片段C#

I'm trying to generate C# that creates a fragment of XML like this.

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



我在考虑使用像这样的:

I was thinking of using something like this:

[XmlArray( "device_list" ), XmlArrayItem("item")]
public ListItem[] device_list { get; set; }



作为属性,与此类声明:

as the property, with this class declaration:

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

    [XmlText]
    public string Value { get; set; }
}



这给了我内在的序列化,但我不知道该怎么适用 TYPE =列表属性添加到 device_list 上方。

我在考虑(但不知道怎么写的语法),我需要做一个:

I'm thinking (but not sure of how to write the syntax) that I need to do a:

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

    [XmlArray]
    public ListItem[] .... This is where I get lost
}

更新基于Dave的回答

Updated based on Dave's responses

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

和类型,而在这样的代码因此被宣称:

And the type, while being declared in the code like thus:

device_list = new DeviceList{type = "list"}
device_list.Add( new ListItem { type = "MAC", Value = "1234566" } );
device_list.Add( new ListItem { type = "MAC", Value = "1234566" } );



没有显示在序列化的类型。这是序列化的结果是:

Isn't showing the type on serialization. This is the result of the serialization:

<device_list>
  <item type="MAC">1234566</item>
  <item type="MAC">1234566</item>
</device_list>



因此很明显,我还是失去了一些东西...

So apparently I'm still missing something...

推荐答案

使用Dave的答案的一部分上面,我发现,这是最好的使用属性在这样的声明类:(注意缺少的属性)

Using part of Dave's answer above, I found that it was best to use the property in the declaring class like this: (note the lack of attributes)

public DeviceList device_list { get; set; }



,然后更新DEVICELIST类是这样的:

and then update the DeviceList class like this:

[XmlType("device_list")]
[Serializable]
public class DeviceList {
    [XmlAttribute]
    public string type { get; set; }

    [XmlElement( "item" )]
    public ListItem[] items { get; set; }
}

和保持原有的ListItem类

and keep the original ListItem class

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

    [XmlText]
    public string Value { get; set; }
}

和我的序列化是预料之中的:

and my serialization is as expected:

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

这篇关于我如何给一个数组的属性在C#中序列化过程中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆