views:

25

answers:

1

Lets say I have this XML file:

<weather>
   <temp>24.0</temp>
   <current-condition iconUrl="http://...."&gt;Sunny&lt;/current-condition&gt;
</weather>

I'm trying to create a C# class to represent this using Attributes in order to call XmlSerializer and have strongly typed tag access. I think the structure will look something like this:

[XmlRoot("weather")]
public class WeatherData
{
    [XmlElement("temp")]
    public string Temp { get; set; }

    [XmlElement("current-condition")]
    public CurrentCondition currentCond = new CurrentCondition();
}

public class CurrentCondition
{
    [XmlAttribute("iconUrl")
    public string IconUrl { get; set; }

    // Representation of Inner Text?
}

Representing the 'temp' tag was straight forward. However, given a tag like current-condition which has both inner text and an attribute, how do I represent the inner text?

I'm likely over-complicating this, so please feel free to suggest an alternative.

+3  A: 

Use [XmlText] to describe the inner text content.

public class CurrentCondition
{
    [XmlAttribute("iconUrl")
    public string IconUrl { get; set; }

    // Representation of Inner Text:
    [XmlText]
    public string ConditionValue { get; set; }
}
John Saunders
I saw this a couple times looking through MSDN; this proves I definitely didn't read it very carefully! Thanks!
Jeff Dalley