2016-12-26 5 views
3

저는 XML을 C# 클래스로 deserializing하고 있습니다.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는 자신의 이름 속성 값을 얻는다. 나머지 속성은 null입니다.

아무도 나에게 잘못을 말할 수 있습니까?

답변

2

시도 다음

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.IO; 
using System.Xml; 
using System.Xml.Serialization; 

namespace ConsoleApplication1 
{ 
    class Program 
    { 
     const string FILENAME = @"c:\temp\test.xml"; 
     static void Main(string[] args) 
     { 
      IMAApplications applications = Deserialize(FILENAME); 
     } 

     static IMAApplications Deserialize(string configFilePath) 
     { 
      IMAApplications 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(IMAApplications), xRoot); 


      using (var stream = File.OpenRead(path)) 
       appConfig = (IMAApplications)serializer.Deserialize(stream); 


      return appConfig; 
     } 
    } 
    [XmlRoot("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; } 
     } 
    } 

    [XmlRoot("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; 
     } 
    } 
    [XmlRoot("Applications")] 
    public class IMAApplications 
    { 
     [XmlElement("Application")] 
     public List<IMAApplication> applications { get; set; } 
    } 

    [XmlRoot("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("InstrumentTypes")] 
    [XmlArrayItem(ElementName = "InstrumentType")] 
    public List<IMAInstrumentType> SupportedInstrumentTypes { get; set; } 

    [XmlArray("ProcessSteps")] 
    [XmlArrayItem(ElementName = "ProcessStep")] 
    public List<IMAProcessStep> ProcessSteps { get; set; } 
    } 
} 
+0

이 코드를 시도하고 결과는 ProcessStep 만 명 속성이 아닌 다른 속성 – Abhash786

+0

마지막 6 줄의 코드 당신이 ProcessSteps 및 채우기입니다 ... 내와 동일 InstrumentTypes 이 전환되었습니다. – jdweng

+0

알았어 ... 고마워 ... .... 지금 일하고있어 – Abhash786