我在
下面有一个XML示例<job>
<refno>XXX</refno>
<specialisms>
<specialism>1</specialism>
<specialism>2</specialism>
</specialisms>
</job>
如何使用WCF c#将这些值序列化到列表中?
我目前有......
[DataMember]
public SpecialismList specialisms { get; set; }
[CollectionDataContract(Name = "specialisms", ItemName = "specialism")]
public class SpecialismList : List<int> { }
但目前还没有工作......任何提示?
答案 0 :(得分:0)
此数据合同应该可以很好地序列化到您发布的XML(请参阅下面的代码)。你遇到的问题是什么?
public class StackOverflow_7386673
{
[DataContract(Name = "job", Namespace = "")]
public class Job
{
[DataMember(Order = 0)]
public string refno { get; set; }
[DataMember(Order = 1)]
public SpecialismList specialisms { get; set; }
}
[CollectionDataContract(Name = "specialisms", ItemName = "specialism", Namespace = "")]
public class SpecialismList : List<int> { }
public static void Test()
{
MemoryStream ms = new MemoryStream();
DataContractSerializer dcs = new DataContractSerializer(typeof(Job));
Job job = new Job
{
refno = "XXX",
specialisms = new SpecialismList { 1, 2 }
};
XmlWriterSettings ws = new XmlWriterSettings
{
OmitXmlDeclaration = true,
Indent = true,
IndentChars = " ",
Encoding = new UTF8Encoding(false),
};
XmlWriter w = XmlWriter.Create(ms, ws);
dcs.WriteObject(w, job);
w.Flush();
Console.WriteLine(Encoding.UTF8.GetString(ms.ToArray()));
}
}