Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using XML class Attributes, how to represent an XML tag with both inner text and attributes?

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.

like image 456
Jeff Dalley Avatar asked Sep 03 '10 17:09

Jeff Dalley


1 Answers

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; }
}
like image 187
John Saunders Avatar answered Oct 23 '22 09:10

John Saunders