我尝试将以下xml结构转换为C#,感谢您的帮助:

<AnimalTypeList>
   <Type>Tiger</Type>
   <Type>Rabbit</Type>
<AnimalTypeList>


到目前为止,我在示例应用程序中定义了以下C#。我很难正确满足上述结构:

public class TestApp
{
  [XmlArrayItem(ElementName="Type")]
  public AnimalTypeListType[] AnimalTypeList {get; set;}
}


这是另一堂课

public class AnimalTypeListType
{
   [XmlElement]
   public string Type {get; set;}
}


这是我的架构:

 <xs:element name="AnimalTypeList" type="AnimalTypeListType"/>
  <xs:complexType name="AnimalTypeListType">
   <xs:sequence>
      <xs:element type="xs:string" name="Type" maxOccurs="unbounded"  minOccurs="0"/>
   </xs:sequence>
  </xs:complexType>

最佳答案

你只需要一堂课

string xml = @"<AnimalTypeList>
                    <Type>Tiger</Type>
                    <Type>Rabbit</Type>
                </AnimalTypeList>";

var serializer = new XmlSerializer(typeof(AnimalList));
var result = (AnimalList)serializer.Deserialize(new MemoryStream(Encoding.UTF8.GetBytes(xml)));


[XmlRoot("AnimalTypeList")]
public class AnimalList
{
    [XmlElement("Type")]
    public string[] Animals;
}

10-06 10:01