2011-10-14 66 views
0

我有一個需要轉換爲對象的xaml文件,以前有沒有人做過?如何將XAML文件轉換爲對象

+0

可能會給你一些見解[如何對反序列化] [1] [1]:http://stackoverflow.com/questions/364253/how-to- deserialize-xml-document –

+0

你應該定義「convert into objects」,爲你需要做的事添加一些細節。精確度是你的朋友。 –

回答

2
using (var stream = File.OpenRead(filename)) { 
    var yourObj = XamlReader.Load(stream); 
} 
0
//Configuration Class 

namespace SKAT.Postfordeler.Shared.DataTypes 
{ 
    [Serializable] 
    public class PostFordelerConfiguration 
    { 

    private readonly ReceiverAddressList _receiverAddresses; 
    private readonly DocumentTypeList _documentTypes; 

    public PostFordelerConfiguration() 
     { 
      _receiverAddresses = new ReceiverAddressList();// I don't want to implement like this. 
      _documentTypes = new DocumentTypeList(); //// I don't want to implement like this. 
     } 


    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 
     public ReceiverAddressList ReceiverAddresses 
     { 
      get { return _receiverAddresses; } 
     } 

    [DesignerSerializationVisibility(DesignerSerializationVisibility.Content)] 
     public DocumentTypeList DocumentTypes { get {return _documentTypes;} } 


    public static PostFordelerConfiguration Load(string location) 
     { 
      return (PostFordelerConfiguration)XamlReader.Load(new XmlTextReader(location)); 
     } 

    } 
} 

//Document Entity 

namespace SKAT.Postfordeler.Shared.DataTypes 
{ 
    [Serializable] 
    public class DocumentType 
    { 
     public String Id { get; set; } 
    } 
} 

//Document List 

namespace SKAT.Postfordeler.Shared.DataTypes 
{ 
    [Serializable] 
    public class DocumentTypeList : List<DocumentType>{ } 
} 


//ReceiverAddress Entities 

namespace SKAT.Postfordeler.Shared.DataTypes 
{ 
    [Serializable] 
    public class ReceiverAddress 
    { 
     public String Id { get; set; } 
     public String Routable { get; set; } 
     public String Description { get; set; } 

    } 
} 

//ReceiverAddress List 

namespace SKAT.Postfordeler.Shared.DataTypes 
{ 
    [Serializable] 
    public class ReceiverAddressList : List<ReceiverAddress>{ } 
} 


// Load XAML file and Convert into objects 


SKAT.Postfordeler.Shared.DataTypes.PostFordelerConfiguration loader = 
       Postfordeler.Shared.DataTypes.PostFordelerConfiguration.Load(
        @"D:\projects\skatpostfordeler\SKAT.Postfordeler.Client.UI\PostfordelerConfiguration.xaml");