2010-09-03 45 views
13

可以說我有這個XML文件:使用XML類屬性,如何使用內部文本和屬性表示XML標籤?

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

我試圖創建一個C#類來表示,以調用XmlSerializer的這種使用屬性,並強類型的標籤訪問。我認爲結構看起來是這樣的:

[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? 
} 

表示'temp'標記是直接的。然而,給定一個像current-condition這樣的既有內部文本又有屬性的標籤,我如何表示內部文本?

我可能過分複雜這個,所以請隨時提出一個替代方案。

回答

20

使用[XmlText]來描述內部文本內容。

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

    // Representation of Inner Text: 
    [XmlText] 
    public string ConditionValue { get; set; } 
} 
+0

我在MSDN上看了幾次,這證明我絕對沒有仔細閱讀它!謝謝! – 2010-09-03 17:52:59