2

I have the following XML:

<Root>
   <EventSet>
      <Event>
         <id>12345</id>
         <rant>
            <localTime>
               <dst>true</dst>
               <time>7/2/2012 14:30</time>
            </localtime>
            <randomRant>
               <random>to illustrate point</random>
               <rant>help me!</rant>
            </randomRant>
         </rant>
      </Event>
      <Event>/*another event*/</Event>
      <Event>/*another event*/</Event> //etc
   </EventSet>
</Root>

I want to map this to:

[Serializable] 
public class Events  
{      
     public List<Event> events { get; set; } 
}  

[Serializable] 
public class Event 
{      
    public int id { get; set; }      
    public Rant rant { get; set; } //this is where I get confused
}

QUESTION: How do I serialize the tags within <rant>? Do I continue to make serialized classes of the parent until the child tags have no children? For example, below:

[Serializable] 
public class Rant
{      
    public LocalTime localTime { get; set; }      
    public RandomRant randomRant { get; set; }
}

[Serializable] 
public class LocalTime
{      
    public bool dst { get; set; }
    public DateTime time { get; set; }
}

[Serializable] 
public class RandomRant
{      
    public string random { get; set; }
    public string rant { get; set; }
}

Or is there a better approach?

EDIT: A given event has one and only one id, and one and only one rant. For the sake of my question, assume my types are valid. I am looking to deserialize nested XML into an object.

3
  • Are localTime and utcTime arrays or single values? Commented Jul 2, 2012 at 19:47
  • I am not sure what your question is. Are you trying to store a single DateTime in two formats, local and UTC? Or do the two DateTimes represent different values (not just TimeZone difference)? Erick Commented Jul 2, 2012 at 19:51
  • I changed my XML. Nothing to do with DateTime formats. I want to know how to deserialize into an object from XML with nested tags. Commented Jul 2, 2012 at 19:55

1 Answer 1

3

The example you gave seems fine to me. As for the time values, the date format in your example is not compatible with xsd:dateTime, so you can't just do

public DateTime time { get; set; }

This will not be serialized. Although you can hack this using a custom XmlSerializer, a more simple approach is to use:

[XmlIgnore]
public DateTime _time {
    get { return DateTime.ParseExact(this.time, "MM/dd/YYYY HH:mm", CultureInfo.InvariantCulture);} // or use some specific culture here.
}

[XmlElement]
public string time { get; set; }

UPDATE: public List<Event> events will be de/serialized as:

<events>
    <Event></Event>
    <Event></Event>
    <Event></Event>
</events>

You can make it as:

[XmlElement("Event")]
public List<Event> events {get; set}

and it will be serialized as

    <Event></Event>
    <Event></Event>
    <Event></Event>

without wrapper.

Or, using:

[XmlArray("EventSet")]
[XmlArrayItem("Event")]
public List<Event> events {get; set}

will be serialized as described in the example

<EventSet>
    <Event></Event>
    <Event></Event>
</EventSet>

And of course the root element:

[XmlRoot("Root")]
public class RootElement{
    [XmlArray("EventSet")]
    [XmlArrayItem("Event")]
    public List<Event> events {get; set}
}
Sign up to request clarification or add additional context in comments.

2 Comments

I am using a proprietary time format and generalized it for my question. I am looking to deserialize into an object from XML with nested tags. Let's assume everything is valid for the sake of my question.
Okay, I've just updated the answer with some examples on how to de/serialize the array elements. Except the arrays - your code seems fine to deserialize this xml.

Your Answer

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