这是我的Model类:
public class ModelClass
{
public ModelClass()
{
}
public ModelClass(string operationName)
{
OperationName = operationName;
}
public string OperationName { get; set; }
public int Count { get; set; }
}
我必须将此模型类的列表序列化为数据库表: 表模式是:
ModelObjectContent (the serialized string)
IDModel
我用来序列化的方法:
public static string SerializeObject(this List<ModelClass> toSerialize)
{
var xmlSerializer = new XmlSerializer(toSerialize.GetType());
var textWriter = new StringWriter();
xmlSerializer.Serialize(textWriter, toSerialize);
return textWriter.ToString();
}
当我将其保存到数据库时,生成的字符串是这样的:
<ArrayOfChartModel>
<ModelClass>
<OperationName>Chart1</OperationName>
<Count >1</Count>
</ModelClass>
<ModelClass>
<OperationName>Chart2</OperationName>
<Count >2</Count>
</ModelClass>
//etc
</ArrayOfChartModel>
框架自动创建一个名为ArrayOfChartModel的标签,没关系,
但我的问题是:
如何将此xml再次实现到ModelClass列表?
答案 0 :(得分:0)
这应该适合你:
public static List<ModelClass> Deserialize(string xml)
{
var xmlSerializer = new XmlSerializer(typeof(ModelClass));
using (TextReader reader = new StringReader(xml))
{
return(List<ModelClass>)xmlSerializer.Deserialize(reader);
}
}