0

I serialize XML files to objects. Let's not talk about how I serialize it as it's not the problem. The problem is how to build a class for complex type elements. For normal XML elements, I do it follows (using System.Xml.Serialization of course)

public class Item
{
    [XmlElement("thumbnail")]
    public string thumbnail { get; set; }
}

All works good. But for a complex type, I don't know how to represent it in a class, I tried to represent it by an array like this

public class Item
{
    [XmlArray("thumbnail")]

    [XmlArrayItem("url")]
    public string url { get; set; }

    [XmlArrayItem("width")]
    public string width { get; set; }

    [XmlArrayItem("height")]
    public string height { get; set; }

    public string[] thumbnail { get; set; }
 }

but this didn't work.

any ideas how to represent an XML complex element in a C# class?

3
  • Can you show sample XML file? Commented Jun 2, 2013 at 12:41
  • Show us the schema and we can help. However the .NET framework/VS SDK comes with a command line tool xsd.exe that can take a schema file schema.xsd and generate C# (or VB) classes from it so you could simply use that tool to generate your classes or at least to have a sample to start with: msdn.microsoft.com/en-us/library/x6c1kb0s%28v=vs.110%29.aspx Commented Jun 2, 2013 at 12:55
  • Thanks for your replies. I edited the post to include the XML file. I know about xsd.exe and I can use that for this purpose but as I have a project running in the tags way I need to do it in the same way. Thanks! Commented Jun 2, 2013 at 13:11

1 Answer 1

1

You should have an Item class, and a Thumbnail class, more like this:

public class item {
    [XmlElement("thumbnail")]
    public thumbnail thumbnail {get;set;}
}

public class thumbnail
{
    [XmlElement("url")]
    public string url { get; set; }

    [XmlElement("width")]
    public string width { get; set; }

    [XmlElement("height")]
    public string height { get; set; }
 }
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.