我有以下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; }
}
答案 0 :(得分:0)
为什么不从原始元素计算它:
[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 };
}
}
答案 1 :(得分:0)
这是另一种方法。我通过使用编辑 - &gt;获得了大部分内容。选择性粘贴 - &gt;在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,
}