我在根节点上列出Snapshots数组时遇到问题。这是代码:
[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; }
}
以下是生成的XML:
<WebService xsd:schemaLocation="..."> <Snapshots/> </WebService>
应该是:
<WebService xsd:schemaLocation="..."> <xsd:Snapshots> <xsd:Snapshot>...</xsd:Snapshot> <xsd:Snapshot>...</xsd:Snapshot> </xsd:Snapshots> </WebService>
答案 0 :(得分:1)
您的快照数组必须为空。否则,实际上会生成元素。
一个注意事项 - 您需要为NodeCreateSnapshot类添加XmlInclude声明,否则会出现运行时异常。您可以这样添加:
[XmlRoot(ElementName = "WebService", DataType = "WebService", IsNullable = false)]
[XmlInclude(typeof(NodeCreateSnapshot))]
public class WebServiceXmlElement
{
// ...
}