1

I have the following xml structure that i am trying to convert to C#, I appreciate the assistance:

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

So far i have the following C# defined in my sample app. I am having difficulty correctly satisfying the above structure:

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

Here is the other class

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

Here is my schema:

 <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>
0

1 Answer 1

3

You only need one class

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;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.