XML反序列化泛型方法 [英] XML deserialization generic method
问题描述
我的下一个XML文件:
I have next XML file:
<Root>
<Document>
<Id>d639a54f-baca-11e1-8067-001fd09b1dfd</Id>
<Balance>-24145</Balance>
</Document>
<Document>
<Id>e3b3b4cd-bb8e-11e1-8067-001fd09b1dfd</Id>
<Balance>0.28</Balance>
</Document>
</Root>
我反序列化这个类:
I deserialize it to this class:
[XmlRoot("Root", IsNullable = false)]
public class DocBalanceCollection
{
[XmlElement("Document")]
public List<DocBalanceItem> DocsBalanceItems = new List<DocBalanceItem>();
}
其中, DocBalanceItem
是:
public class DocBalanceItem
{
[XmlElement("Id")]
public Guid DocId { get; set; }
[XmlElement("Balance")]
public decimal? BalanceAmount { get; set; }
}
下面是我的反序列化方法:
Here is my deserialization method:
public DocBalanceCollection DeserializeDocBalances(string filePath)
{
var docBalanceCollection = new DocBalanceCollection();
if (File.Exists(filePath))
{
var serializer = new XmlSerializer(docBalanceCollection.GetType());
TextReader reader = new StreamReader(filePath);
docBalanceCollection = (DocBalanceCollection)serializer.Deserialize(reader);
reader.Close();
}
return docBalanceCollection;
}
所有工作正常,但我有很多的XML文件。除了写作项目
类我必须写 ItemCollection
班为他们每个人。而且我也必须执行 DeserializeItems
方法每个。
All works fine but I have many XML files. Besides writing Item
classes I have to write ItemCollection
classes for each of them. And also I have to implement DeserializeItems
method for each.
我可以反序列化我的XML文件,而无需创建 ItemCollection
班?而且我可以写一个通用的方法来反序列化他们的所有?
Can I deserialize my XML files without creating ItemCollection
classes? And can I write single generic method to deserialize all of them?
想到的唯一的解决办法 - 做一个接口,所有这些类。任何想法?
The only solution that comes to mind - make an interface for all these classes. Any ideas?
推荐答案
您可以反序列化的通用名单,其中,T&GT;
蛮好用的XmlSerializer。但是,首先你需要将 XmlType将
属性添加到您的 DocBalanceItem
所以它知道如何列表中的元素被命名为
You can deserialize a generic List<T>
just fine with XmlSerializer. However, first you need to add the XmlType
attribute to your DocBalanceItem
so it knows how the list elements are named.
[XmlType("Document")]
public class DocBalanceItem
{
[XmlElement("Id")]
public Guid DocId { get; set; }
[XmlElement("Balance")]
public decimal? BalanceAmount { get; set; }
}
然后修改 DeserializeDocBalances()
方法返回一个名单,其中,T&GT;
,并通过串行器的 XmlRootAttribute
实例来指导它来寻找根
作为根元素:
Then modify your DeserializeDocBalances()
method to return a List<T>
and pass the serializer an XmlRootAttribute
instance to instruct it to look for Root
as the root element:
public List<T> DeserializeList<T>(string filePath)
{
var itemList = new List<T>();
if (File.Exists(filePath))
{
var serializer = new XmlSerializer(typeof(List<T>), new XmlRootAttribute("Root"));
TextReader reader = new StreamReader(filePath);
itemList = (List<T>)serializer.Deserialize(reader);
reader.Close();
}
return itemList;
}
那么你应该能够做到
Then you should be able to do
VAR列表= DeserializeList&LT; DocBalanceItem&GT;(somefile.xml);
由于该方法现在返回一个泛型名单,其中,T&GT;
,您不再需要创建自定义集合对于每一种类型的
Since the method now returns a generic List<T>
, you no longer need to create custom collections for every type.
P.S。 - 我测试了该解决方案在本地使用提供的文档,它的工作
P.S. - I tested this solution locally with the provided document, it does work.
这篇关于XML反序列化泛型方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!