Matheus Simon
Matheus Simon

Reputation: 696

Not listing XML elements as array

I'm having a problem with listing the Snapshots array on the root node. Here's the code:

[XmlRoot(ElementName = "WebService", DataType = "WebService", IsNullable = false)]
public class WebServiceXmlElement
{
    [XmlArray("Snapshots"), XmlArrayItem(ElementName = "Snapshot", Namespace = XmlNamespaceSettings.DefaultNamespace, IsNullable = true)]
    public NodeSnapshotElement[] Snapshots { get; set; }
}

public class NodeSnapshotElement
{ 

}

[XmlType(TypeName = "CrateSnapshot", Namespace = XmlNamespaceSettings.DefaultNamespace, IncludeInSchema = false)]
public class NodeCreateSnapshot : NodeSnapshotElement
{
    [XmlAttribute(AttributeName = "ID", Namespace = XmlNamespaceSettings.DefaultNamespace)]
    public string ID { get; set; }
}

And here's the resulting XML:

<WebService xsd:schemaLocation="...">
  <Snapshots/>
</WebService>

It was supposed to be:

<WebService xsd:schemaLocation="...">
  <xsd:Snapshots>
    <xsd:Snapshot>...</xsd:Snapshot>
    <xsd:Snapshot>...</xsd:Snapshot>
  </xsd:Snapshots>
</WebService>

Upvotes: 0

Views: 50

Answers (1)

dotnetom
dotnetom

Reputation: 24901

Your Snapshots array must be empty. Otherwise the elements are actually generated.

One note - you need to add the XmlInclude declaration for NodeCreateSnapshot class, otherwise you get a runtime exception. You can add it like this:

[XmlRoot(ElementName = "WebService", DataType = "WebService", IsNullable = false)]
[XmlInclude(typeof(NodeCreateSnapshot))]
public class WebServiceXmlElement
{
    // ...
}

Upvotes: 1

Related Questions