Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8920523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:17:35+00:00 2026-06-15T06:17:35+00:00

I am trying to save the WPF code that i have inside a Canvas

  • 0

I am trying to save the WPF code that i have inside a Canvas with serialization.

What I want to do is that i can add elements in my C# program and then when I press save I want to save the content of my canvas to a file.

I am using the SaveFileDialog to first open a Dialog were i can save the file.

But when I am trying to save it I have problems.

        Microsoft.Win32.SaveFileDialog dlg = new Microsoft.Win32.SaveFileDialog();
        dlg.FileName = "Name_" + random_save_name; 
        dlg.DefaultExt = ".text";
        dlg.Filter = "Text documents (.txt)|*.txt";
        Nullable<bool> result = dlg.ShowDialog();

        if (result == true)
        {
            SerializeToXML(this.parentWindow, this.parentWindow.canvas, 96, dlg.FileName);
        }

canvas is the name of my canvas, and the SerializeToXML locks like this.

    public static void SerializeToXML(MainWindow window, Canvas canvas, int dpi, string filename)
    {
        XmlSerializer serializer = new XmlSerializer(typeof(Canvas));
        TextWriter textWriter = new StreamWriter(filename);
        serializer.Serialize(textWriter, canvas);
        textWriter.Close();
     }

I can run the program but i get an Exception. I am probably doing this wrong but i have had a hard time finding a solution to this.

Eddt:
“There was an error reflecting type ‘System.Windows.Controls.Canvas’.” and “Exception Intercepted: SerialixeToXML, too An exception was intercepted and the call stack unwound to the point before the call from user code where the exception occured. “Unwind the call stack on unhandled exceptions” is selected in the debugger options” i am not the best reading from the debugger in VS, that is what the live Event says

Edit 2: did find this: http://blogs.msdn.com/b/ashish/archive/2008/01/15/dynamically-producing-xaml-files-using-xamlwriter-save-method.aspx and now i can save it to an txt file.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:17:37+00:00Added an answer on June 15, 2026 at 6:17 am

    What i did was:

        public static void SerializeToXML(MainWindow window, Canvas canvas, int dpi, string filename)
        {
            string mystrXAML = XamlWriter.Save(canvas);
            FileStream filestream = File.Create(filename);
            StreamWriter streamwriter = new StreamWriter(filestream);
            streamwriter.Write(mystrXAML);
            streamwriter.Close();
            filestream.Close();
        }
    

    This way i can save the canvas.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

When trying to save the database using boost serialization, I encounter the segfault that
i am trying to rotate elements on a canvas and the save their rotated
i have a VSTO application(WPF) which can save an opened Excel File on a
I'm trying to understand how image serialization works in WPF. I have the following
I have a c# WPF application that needs to save data when it closes.
I have a WPF form that has some buttons to save user input, delete,
Im trying to save a canvas div and then upload it to the server
I'm trying to save my Photo class that has a byte[] File field. When
I'm trying to save a huge NSArray to a file and then retrieve it...
I am trying to save multiple image using this code. But instead its saving

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.