1

我正在反序列化一个 xml,为此我使用这些类

[DataContract(Name = "Validation")]
public class FieldValidationModel
{
    [DataMember]
    public string Annotation { get; set; }

    [DataMember]
    public Collection<ValidationPlanType> PlanTypes { get; set; }
}

[DataContract(Name="PlanType")]
public class ValidationPlanType
{
    [DataMember]
    public string Name { get; set; }
}

它有效,但理想情况下我只想使用一个类

[DataContract(Name = "Validation")]
public class FieldValidationModel
{
    [DataMember]
    public string Annotation { get; set; }

    [DataMember]
    public List<string> PlanTypes { get; set; }
} 

我试过用

[DataMember]
[XmlArray, XmlArrayItem(Namespace="PlanType")]
public List<string> PlanTypes { get; set; }

没有运气,如果我能这样做,节点将从我现在必须使用的东西中变平

<Validation>
  <Annotation>TestAnnotation</Annotation>
  <PlanTypes>
    <PlanType>
      <Name>PlanA</Name>
    </PlanType>
    <PlanType>
      <Name>PlanB</Name>
    </PlanType>
  </PlanTypes>
</Validation>

更易读

<Validation>
  <Annotation>TestAnnotation</Annotation>
  <PlanTypes>
    <PlanType>PlanA</PlanType>
    <PlanType>PlanB</PlanType>
  </PlanTypes>
</Validation>

谢谢

4

1 回答 1

0

我没有尝试过,但应该可以:

[DataContract(Name = "Validation")]
public class FieldValidationModel
{
      [DataMember]
      public string Annotation { get; set; }

      [DataMember]
      public PlanTypeCollection PlanTypes { get; set; }
}

[CollectionDataContract(ItemName="PlanType")]
public class PlanTypeCollection : Collection<string>  {}
于 2013-06-05T11:56:51.223 回答