如何将XAML文件转换为对象

时间:2011-10-14 03:03:10

标签: xaml

我有一个需要转换为对象的xaml文件,之前是否有人这样做过?

2 个答案:

答案 0 :(得分:2)

using (var stream = File.OpenRead(filename)) {
    var yourObj = XamlReader.Load(stream);
}

答案 1 :(得分: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");