开发者

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

开发者 https://www.devze.com 2023-01-14 14:37 出处:网络
Lets say I have this XML file: <weather> <temp>24.0</temp> <current-condition iconUrl=\"http://....\">Sunny</current-condition>

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开发者_Python百科 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; }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消