我有這樣的類反序列化我的課:如何反序列化類的特定項目的子集
public class Serializer <T> {
public T SerializeAppPath(string xmlpath) {
T apppath;
using (TextReader txtReader = new StreamReader(xmlpath)) {
XmlSerializer xmlSerializer = new XmlSerializer(typeof(T));
apppath = (T)xmlSerializer.Deserialize(txtReader);
return apppath;
}
}
}
這裏是一流的,我想將XML反序列化爲:
[XmlRoot("KPIPATHS")]
public sealed class KpiPath
{
public sealed class KPI
{
[XmlElement("Name")]
public string Name { get; set; }
[XmlElement("RequestTemplateFullPath")]
public string RequestTemplateFullPath { get; set; }
[XmlElement("RequestFullPath")]
public string RequestFullPath { get; set; }
public KPI()
{
}
}
[XmlArray("KPIS")]
public List<KPI> Kpis { get; set; }
public KpiPath()
{
}
}
以下是我如何執行它以從XML獲取結果。我是「Kpis」的撤回清單,我的問題是我想從中取得只有一個KPI
設置基於條件"when Name = something"
然後我想使用和進一步通過,KpiPath
類。希望你明白我的觀點。
Serializer<KpiPath> serializer2 = new Serializer<KpiPath>();
kpiPath = serializer2.SerializeAppPath(KpiPaths);
一些示例XML:
<?xml version="1.0" encoding="utf-8"?>
<KPIPATHS>
<KPIS>
<KPI>
<Name>AXD</Name>
<RequestTemplateFullPath>D:\something</RequestTemplateFullPath>
<RequestFullPath>D:\something</RequestFullPath>
<ResultFullPath>D:\something</ResultFullPath>
</KPI>
<KPI>
<Name>DRE</Name>
<RequestTemplateFullPath>D:\something</RequestTemplateFullPath>
<RequestFullPath>D:\something</RequestFullPath>
<ResultFullPath>D:\something</ResultFullPath>
</KPI>
</KPIS>
能否請您包括一些示例XML? – xDaevax 2014-10-02 12:45:15
您可以手動解析'xml'(壞),或者您可以反序列化所有內容(整個'KPIPATHS')並稍後執行條件過濾。 – Sinatr 2014-10-02 13:23:02
不,我想避免xml reader或類似的東西。序列化的方式是我選擇的。 – Bob 2014-10-02 13:25:25