3
我反序列化我的xml到C#類。XMLAttribute不工作
<?xml version="1.0" encoding="UTF-8"?>
<Applications>
<Application Name = "name1" MakerCheckerType="Operational" SanctionCheckNeeded="N" PreExistCheckNeeded="N" >
<InstrumentTypes>
<InstrumentType Version="1.0" PrimaryKeyExcel="Security Name" Name="Equity">
</InstrumentType>
<InstrumentType Name="Bond" Version="1.0" PrimaryKeyExcel="Security Name">
</InstrumentType>
</InstrumentTypes>
<ProcessSteps>
<ProcessStep VBAFunction="xyz" ExcelName="xyz" Name="Upload" />
<ProcessStep Name ="Approve_Reject" VBAFunction="xyz" ExcelName="xyz"/>
</ProcessSteps>
</Application>
<Application Name = "name2" MakerCheckerType="Real" SanctionCheckNeeded="Y" PreExistCheckNeeded="Y">
<InstrumentTypes>
<InstrumentType Version="1.0" PrimaryKeyExcel="Security Name" Name="Equity">
</InstrumentType>
<InstrumentType Name="Bond" Version="1.0" PrimaryKeyExcel="Security Name">
</InstrumentType>
</InstrumentTypes>
<ProcessSteps>
<ProcessStep VBAFunction="xyz" ExcelName="xyz" Name="Upload" />
<ProcessStep Name ="Approve_Reject" VBAFunction="xyz" ExcelName="xyz"/>
</ProcessSteps>
</Application>
</Applications>
類:
[XmlType("ProcessStep")]
public class IMAProcessStep
{
private string name;
private string vbaFunction;
private string excelName;
[XmlAttribute("Name")]
public string Name
{
get { return name; }
set { name = value; }
}
[XmlAttribute("VBAFunction")]
public string VBAFunction
{
get { return vbaFunction; }
set { vbaFunction = value; }
}
[XmlAttribute("ExcelName")]
public string ExcelName
{
get { return excelName; }
set { excelName = value; }
}
}
[XmlType("InstrumentType")]
public class IMAInstrumentType
{
[XmlAttribute("Name")]
public string Name
{
get;
set;
}
[XmlAttribute("Version")]
public string Version
{
get;
set;
}
[XmlAttribute("PrimaryKeyExcel")]
public string PrimaryKeyExcel
{
get;
set;
}
}
[XmlType("Application")]
public class IMAApplication
{
[XmlAttribute("Name")]
public string Name { get; set; }
[XmlAttribute("MakerCheckerType")]
public string MakerCheckerType { get; set; }
public bool IsMakerCheckerType
{
get
{
if (MakerCheckerType == "Real")
return true;
return false;
}
set
{
if (value)
MakerCheckerType = "Real";
else
MakerCheckerType = "Operational";
}
}
[XmlAttribute("SanctionCheckNeeded")]
public string SanctionCheckNeeded { get; set; }
[XmlAttribute("PreExistCheckNeeded")]
public string PreExistCheckNeeded { get; set; }
public bool IsSanctionCheckNeeded
{
get
{
return SanctionCheckNeeded == "Y";
}
set
{
SanctionCheckNeeded = value ? "Y" : "N";
}
}
public bool IsPreExistCheckNeeded
{
get
{
if (PreExistCheckNeeded == "Y")
return true;
return false;
}
set
{
if (value)
PreExistCheckNeeded = "Y";
else
PreExistCheckNeeded = "N";
}
}
[XmlArray("ProcessSteps")]
[XmlArrayItem(ElementName = "ProcessStep")]
public List<IMAInstrumentType> SupportedInstrumentTypes { get; set; }
[XmlArray("InstrumentTypes")]
[XmlArrayItem(ElementName = "InstrumentType")]
public List<IMAProcessStep> ProcessSteps { get; set; }
}
這裏如何我德其序列...
List<IMAApplication> appConfig = null;
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, configFilePath);
var xRoot = new XmlRootAttribute();
xRoot.ElementName = "Applications";
xRoot.IsNullable = true;
var serializer = new XmlSerializer(typeof(List<IMAApplication>), xRoot);
using (var stream = File.OpenRead(path))
appConfig = (List<IMAApplication>)serializer.Deserialize(stream);
return appConfig;
IMAApplication反序列化成功,但processSteps和InstrumentTypes只能得到他們的名稱屬性值。其餘屬性爲空。
有誰能告訴我這裏有什麼問題。
嘗試這個代碼和結果是和我一樣......在ProcessStep只有名稱屬性填充,沒有其他屬性 – Abhash786
最後6行的代碼,你有ProcessSteps和InstrumentTypes 切換。 –
jdweng
知道了......非常感謝......現在正在工作 – Abhash786