may I know how to serialize and deserialize using C# if there have dynamic number behind of the element ? Block1 and Block2 will be dynamic based on the Block Count.
<DefectList>
<BlockCount>2</BlockCount>
<Block1>
</Block1>
<Block2>
</Block2>
</DefectList>
Thanks.
public static T Deserialize<T>(this T Value, string xmlPath)
{
T ret = default(T);
if (!string.IsNullOrEmpty(xmlPath))
{
var serializer = new XmlSerializer(typeof(T));
using (var tw = new FileStream(xmlPath, FileMode.Open))
{
ret = (T)serializer.Deserialize(tw);
tw.Close();
}
}
return ret;
}
I'm trying to use this method to deserialize. and my model as below
public class DefectList
{
[XmlElement("DefectList")]
public string DefectList{ get; set; }
[XmlElement("BlockCount")]
public int BlockCount { get; set; }
[XmlElement]
public ObservableCollection<Block> Block { get; set; }
}
XmlSerializercan seductively lead you to the darkside.