如何表示以下XML的结构,以便进一步反序列化为类?
<HeadElement id="1">
Text in HeadElement start
<SubElement samp="0">
Text in SubElement
</SubElement>
Continue text
</HeadElement>
我目前的代码如下:
[DataContract]
public class ClaimText
{
[DataMember, XmlElement(ElementName = "claim-ref")]
public ClaimRef claimref; // { get; private set; }
public void setclaimref(ClaimRef claimref_)
{
this.claimref = claimref_;
}
[DataMember, XmlText()]
public string txt; // { get; private set; }
public void settxt(string txt_)
{
this.txt = txt_;
}
}
给出以下XML内容:
<claim id="CLM-00016" num="00016">
<claim-text>16. The midgate assembly of <claim-ref idref="CLM-00015">claim 15</claim-ref>, further comprising a second ramp member connected to an opposite side of the midgate panel for selectively covering an opposite end of the pass-through aperture. </claim-text>
</claim>
我得到的链接指向&#34; claim-ref&#34;存在,但不是整个文本:只有它的第二部分(&#34;,进一步包括......&#34;)。如何获得全文?
答案 0 :(得分:1)
首先,您要混合DataContractSerializer
和XmlSerializer
的属性。
Ad rem:如果是混合元素,最好使用XmlSerializer
。以下是适用于您的XML的结构:
[XmlRoot(ElementName = "claim")]
public class ClaimText
{
[XmlAttribute]
public string id;
[XmlAttribute]
public string num;
[XmlElement(ElementName = "claim-text")]
public ClaimInnerContents contents;
}
public class ClaimInnerContents
{
[XmlElement(ElementName = "claim-ref", Type = typeof(ClaimRef))]
[XmlText(Type = typeof(string))]
public object[] contents;
}
public class ClaimRef
{
[XmlAttribute]
public string idref;
[XmlText]
public string text;
}
ClaimRef
和分割的文本部分作为对象反序列化为contents
数组。
您当然可以(并且应该)为此数组的特定元素提供静态类型和选中的访问器。