我有以下 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;
}
关于c# - C# 中的 XML 序列化数组问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40618117/