Lets say I have this XML file:
<weather>
<temp>24.0</temp>
<current-condition iconUrl="http://....">Sunny</current-condition>
</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.
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; }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With