---
title: "C# - load xaml file at runtime"  
description: "C# - load xaml file at runtime"  
author: "Anonymous User"  
published: 2015-03-02  
updated: 2015-03-03  
canonical: https://www.mindstick.com/forum/12996/c-sharp-load-xaml-file-at-runtime  
category: "c#"  
tags: ["c#", ".net", "wpf", "xaml"]  
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](https://www.mindstick.com/articles/59/encrypting-and-decrypting-files-using-c-sharp) on my [disk](https://answers.mindstick.com/qa/96923/what-are-hard-disk-partitions) which I want to [load](https://answers.mindstick.com/qa/33737/what-is-meant-by-average-load-time) at [runtime](https://www.mindstick.com/articles/52/creating-timer-at-runtime-in-c-sharp-dot-net):

```
<Window x:Class="MainWindow"   
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"    Title="I want
to load this xaml file"></Window>                                      
```

How can I load that 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 Anonymous User

```
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/12996/c-sharp-load-xaml-file-at-runtime

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
