---
title: "C# - load xaml file at runtime"  
description: "C# - load xaml file at runtime"  
author: "Anonymous User"  
published: 2013-09-23  
updated: 2013-09-23  
canonical: https://www.mindstick.com/forum/1485/c-sharp-load-xaml-file-at-runtime  
category: "wpf"  
tags: ["wpf"]  
reading_time: 1 minute  

---

# C# - load xaml file at runtime

I have a WPF [application in C#](https://www.mindstick.com/interview/34012/how-do-you-create-a-windows-forms-application-in-c-sharp).

I have a MainWindow [class](https://www.mindstick.com/blog/165/generic-class-in-c-sharp) which inherits from a System.Windows.[Window](https://www.mindstick.com/forum/161285/how-does-the-window-console-object-work-in-javascript) class.

Next I have an xaml file on my disk which I want to load at runtime:

<Window x:Class="MainWindow"

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

[Title](https://www.mindstick.com/articles/12860/how-to-get-financing-for-salvage-title-cars)="I want to load this xaml file">

</Window>

How can I load that [xaml file at runtime](https://www.mindstick.com/forum/12996/c-sharp-load-xaml-file-at-runtime)? In other words, I want my MainWindow class to use exactly the mentioned xaml file, so I do not want to use the [method](https://www.mindstick.com/forum/166/webservice-method) AddChild of the MainWindow because it adds a child to the window, but I want to [replace](https://yourviews.mindstick.com/view/81330/cow-antibody-research-in-usa-a-step-ahead-to-replace-plasma-therapy) also that Window parameter. How can I achieve this?

## Replies

### Reply by Sumit Kesarwani

Hi Jeet,

```
private void ChangeXaml(){    var reader =new StringReader(xamlToReplaceStuffWith);    var xmlReader =XmlReader.Create(reader);    var newWindow =XamlReader.Load(xmlReader) as Window;       
newWindow.Show();    foreach(var prop in typeof(Window).GetProperties())    {       
if(prop.CanWrite)        {            try             {                //A bunch of these will fail. a bunch.               
Console.WriteLine("Setting prop:{0}", prop.Name);               
prop.SetValue(this, prop.GetValue(newWindow, null), null);            } catch            {            }        }    }   
newWindow.Close();   
this.InvalidateVisual();}
```


---

Original Source: https://www.mindstick.com/forum/1485/c-sharp-load-xaml-file-at-runtime

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
