C# 反序列化没有列表根成员的XElement列表
在我的例子中,我有一个XMLTAGC# 反序列化没有列表根成员的XElement列表,c#,xml,silverlight-5.0,xml-deserialization,C#,Xml,Silverlight 5.0,Xml Deserialization,在我的例子中,我有一个XMLTAG,它可以包含一个或多个XMLTAG,但它们可以以随机顺序出现在phraseTag值中。 这里是xmlcode的一个示例: <phrase level="1"> Where <q>are</q> <subject>you</subject> <q>going?</q> </phrase> 如何反序列化标记? 我已尝试使用XmlArrayAttib
,它可以包含一个或多个XMLTAG
,但它们可以以随机顺序出现在phraseTag值中。
这里是xmlcode的一个示例:
<phrase level="1">
Where
<q>are</q>
<subject>you</subject>
<q>going?</q>
</phrase>
如何反序列化
标记?
我已尝试使用
XmlArrayAttibute
和XmlArrayItemAttibute
,但我没有此列表的成员(例如QTags)。您需要xmlement
属性:)
[XmlRoot(“短语”)]
公共类短语
{
[XmlElement(“q”)]
公共列表q{get;set;}
[XmlAttribute(“级别”)]
公共整数级别{get;set;}
[XmlElement(“主题”)]
公共字符串主题{get;set;}
[XmlText]
公共字符串值{get;set;}
}
使用XmlArray和XmlArrayItem非常感谢您的回答,但我正在我的课堂上寻找类似的内容:List Quoted{get;set;}。如何在此类属性上使用XmlElementAttribute?抱歉!我还没见过那房子!不管怎样,这对我不起作用。反序列化后,我的列表q始终计数=0!不!我忘记了“公共”关键字!!!!!!!!!非常感谢,我是个白痴程序员:D
[XmlRoot("phrase")]
public class Phrase
{
public Phrase()
{
this.Quoted = new List<string>();
}
[XmlAttribute("level")]
public int Level { get; set; }
[XmlElement("subject")]
public string Subject { get; set; }
[XmlText]
public string Value { get; set; }
[XmlElement("q")]
List<string> Quoted { get; set; }
}
public static T Deserialize<T>(this XElement xElement)
{
try
{
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
return (T)xmlSerializer.Deserialize(xElement.CreateReader());
}
catch (Exception)
{
throw;
}
}
Phrase.Level = 1
Phrase.Subyect = "you"
Phrase.Value = "Where"
[XmlRoot("phrase")]
public class Phrase
{
[XmlElement("q")]
public List<string> q { get; set; }
[XmlAttribute("level")]
public int Level { get; set; }
[XmlElement("subject")]
public string Subject { get; set; }
[XmlText]
public string Value { get; set; }
}