簡體   English   中英

將多個xml元素反序列化為單個對象屬性

[英]Deserialize multiple xml elements to single object property

我有以下xml

  <Amendment.Text>
    <Page>4</Page>
    <Line>4</Line>
    <Bold>It is a </Bold>   
    <Italic>Beautiful Day</Italic>
    <Bold>In London</Bold>
    <Italic>17 June 2015</Italic>   
</Amendment.Text>

我希望將<Bold><Italic>元素值反序列化為單個字符串數組屬性

我的可序列化類如下,並且不起作用。

[Serializable]
    public class AmdTextType
    {

        public string Page { get; set; }

        public string Line { get; set; }


        public string[] Content { get; set; }


    }

為什么不根據原始元素進行計算:

[XmlElementAttribute("Italic", typeof(string))]        
public string Italic { get; set; }

[XmlElementAttribute("Bold", typeof(string))]
public string Bold { get; set; }

public string[] Content { 
    get {
        return new string[] { this.Italic, this.Bold };
    }
}

這是另一種方法。 通過在Visual Studio中使用“ 編輯”->“選擇性粘貼”->“將XML作為類粘貼” ,可以得到大部分信息。 這還包括一個名稱數組,以防您需要參考粗體或斜體

[XmlType(AnonymousType = true)]
[XmlRoot("Amendment.Text", Namespace = "", IsNullable = false)]
public partial class AmendmentText
{
    [XmlElement("Line", typeof(int))]
    public int Line { get; set; }

    [XmlElement("Page", typeof(int))]
    public int Page { get; set; }

    [XmlElement("Bold", typeof(string))]        
    [XmlElement("Italic", typeof(string))]        
    [XmlChoiceIdentifier("ContentName")]
    public object[] Content { get; set; }

    [XmlElement("ContentName")]
    [XmlIgnore()]
    public ContentName[] ContentName { get; set; }

}

[XmlType(IncludeInSchema = false)]
public enum ContentName
{        
    Bold,
    Italic,        
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM