开发者

Serialize elements of an array but not the array (XMLSerizlization)

I have a simple array that I need to serialize as part of a larger object.

public class Holder
{
    开发者_JAVA百科public int ID { get; set; }
    public string Name { get; set; }
    public Thing[] Thingies { get; set; }
}

public class Thing {}

Normally this would be serialized as:

...
<Holder>
    <ID>...</ID>
    <Name>...</Name>
    <ArrayOfThing>
        <Thing>...</Thing>
        <Thing>...</Thing>
        <Thing>...</Thing>
        ...
    </ArrayOfThing>
</Holder>

Without worrying too much about deserialization, is there a way I could simply remove the ArrayOf element, but keep the elements inside, so that I'd have:

...
<Holder>
    <ID>...</ID>
    <Name>...</Name>
    <Thing>...</Thing>
    <Thing>...</Thing>
    <Thing>...</Thing>
    ...
</Holder>


Try

public class Holder
{
    public int ID { get; set; }
    public string Name { get; set; }

    [XmlElement("Thing")]
    public Thing[] Thingies { get; set; }
}

MSDN for XmlElementAttribute has some examples as well.


You could implement IXmlSerializable to let you read and write Thing or other children from the containing XML element.

Here is how you would implement this Proper way to implement IXmlSerializable?


Use the [XmlElement] attribute:

[XmlElement]
public Thing[] Thingies { get; set; }  
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜