locked
XamlParseException occured. Root element is missing. RRS feed

  • Question

  • Actually what I want to try is to store a Xaml for StackPanel as a file and then access the same file to again load StackPanel and all its content.

    Hi there I have one stack panel - topPanel inside which there is another stack panel - MainPanel. There is one button in MainPanel which gets the Xaml code for MainPanel writes it to the file in D:\ and loads it in UIElement and attepts to add that element to topPanel so that the same Ui will be added to the topPanel. But while loading file in XamlReader I am getting above exception occured.

    XAML

            

    <Window x:Class="WpfApplication2.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="693.5" Width="558.5">
        <StackPanel x:Name="topPanel">
    <StackPanel x:Name="MainPanel" Margin="8,0,0,0" Height="312">
    <Label Content="Write text here to test binding"/>
    <TextBox x:Name="TestInput" Height="64" />
    <TextBlock x:Name="TestStatic" Height="55" />
    <Button Content="Press to see XAML code" Click="ShowXAML"/>
    <TextBox x:Name="XamlCode" Height="100" />

    </StackPanel>
    </StackPanel>
    </Window>

            

    <Window x:Class="WpfApplication2.Window1"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="Window1" Height="693.5" Width="558.5">
        <StackPanel x:Name="topPanel">
    <StackPanel x:Name="MainPanel" Margin="8,0,0,0" Height="312">
    <Label Content="Write text here to test binding"/>
    <TextBox x:Name="TestInput" Height="64" />
    <TextBlock x:Name="TestStatic" Height="55" />
    <Button Content="Press to see XAML code" Click="ShowXAML"/>
    <TextBox x:Name="XamlCode" Height="100" />

    </StackPanel>
    </StackPanel>
    </Window>

    XAML.CS

    using System;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Input;
    using System.Windows.Markup;
    using System.Text;
    using System.IO;
    using System.Xml;
    using BindingsHelpers;


    namespace WpfApplication2
    {
        /// <summary>
        /// Interaction logic for Window1.xaml
        /// </summary>
        public partial class Window1 : Window
        {
            public Window1()
            {
                InitializeComponent();
                EditorHelper.Register<BindingExpression, BindingConvertor>();
                Binding myBinding = new Binding("Text");
                myBinding.Source = TestInput;
                TestStatic.SetBinding(TextBlock.TextProperty, myBinding);


            }
            void ShowXAML(object sender, RoutedEventArgs e)
            {
                TextWriter writer = File.CreateText("d:\\xamlfile.xaml");
                //StringBuilder outstr=new StringBuilder();
                XamlCode.Clear();
                //this code need for right XML fomating 
                XmlWriterSettings settings = new XmlWriterSettings();
                settings.Indent = true;
                settings.OmitXmlDeclaration = true;
                XamlDesignerSerializationManager dsm = new XamlDesignerSerializationManager(XmlWriter.Create(writer, settings));
                //this string need for turning on expression saving mode 
                dsm.XamlWriterMode = XamlWriterMode.Expression;
              
                XamlWriter.Save(MainPanel, dsm);
                //XamlCode.Text = outstr.ToString();

                writer.Close();
                TextReader reader = File.OpenText(@"d:\xamlfile.xaml");
                string outstr = reader.ReadToEnd();
                XamlCode.Text = outstr;

                XmlReaderSettings xrs = new XmlReaderSettings();
                xrs.ValidationType = ValidationType.None;
                XmlReader xmlReader = XmlReader.Create(reader, xrs);

                UIElement obj = (UIElement)XamlReader.Load(xmlReader);  //here is the exception: Root element is missing.
                topPanel.Children.Add(obj);

            }
        }
    }

    Sunday, March 11, 2012 9:14 PM

Answers

  • This would appear to work:

      private void ShowXAML(object sender, RoutedEventArgs e)
      {
       TextWriter writer = File.CreateText("xamlfile.xaml");
       //StringBuilder outstr=new StringBuilder();
       //XamlCode.Clear();
       topPanel.Children.Clear();
    
       //this code need for right XML fomating 
       //XmlWriterSettings settings = new XmlWriterSettings();
       //settings.Indent = true;
       //settings.OmitXmlDeclaration = true;
       //XamlDesignerSerializationManager dsm = new XamlDesignerSerializationManager(XmlWriter.Create(writer, settings));
       //this string need for turning on expression saving mode 
       //dsm.XamlWriterMode = XamlWriterMode.Expression;
    
       //XamlWriter.Save(MainPanel, dsm);
       writer.Write(XamlWriter.Save(MainPanel));
    
       writer.Close();
       TextReader reader = File.OpenText("xamlfile.xaml");
       //string outstr = reader.ReadToEnd();
       //XamlCode.Text = outstr;
    
       //XmlReaderSettings xrs = new XmlReaderSettings();
       //xrs.ValidationType = ValidationType.None;
       //XmlReader xmlReader = XmlReader.Create(reader, xrs);
       XmlReader xmlReader = XmlReader.Create(reader);
    
       UIElement obj = (UIElement)XamlReader.Load(xmlReader);  //here is the exception: Root element is missing.
       topPanel.Children.Add(obj); 
      }
    


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    • Proposed as answer by TSoftware-Old Saturday, March 17, 2012 2:06 PM
    • Marked as answer by Kee Poppy Wednesday, March 21, 2012 2:01 AM
    Sunday, March 11, 2012 10:11 PM
  • Or, ot preserve your XamlDesignerSerilizationManager settings:

      private void ShowXAML(object sender, RoutedEventArgs e)
      {
       TextWriter writer = File.CreateText("xamlfile.xaml");
       //StringBuilder outstr=new StringBuilder();
       //XamlCode.Clear();
       topPanel.Children.Clear();
    
       //this code need for right XML fomating 
       //XmlWriterSettings settings = new XmlWriterSettings();
       //settings.Indent = true;
       //settings.OmitXmlDeclaration = true;
       //XamlDesignerSerializationManager dsm = new XamlDesignerSerializationManager(XmlWriter.Create(writer, settings));
       XamlDesignerSerializationManager dsm = new XamlDesignerSerializationManager(XmlWriter.Create(writer));
       //this string need for turning on expression saving mode 
       dsm.XamlWriterMode = XamlWriterMode.Expression;
    
       XamlWriter.Save(MainPanel, dsm);
       //writer.Write(XamlWriter.Save(MainPanel,));
    
       writer.Close();
       TextReader reader = File.OpenText("xamlfile.xaml");
       //string outstr = reader.ReadToEnd();
       //XamlCode.Text = outstr;
    
       //XmlReaderSettings xrs = new XmlReaderSettings();
       //xrs.ValidationType = ValidationType.None;
       //XmlReader xmlReader = XmlReader.Create(reader, xrs);
       XmlReader xmlReader = XmlReader.Create(reader);
    
       UIElement obj = (UIElement)XamlReader.Load(xmlReader);  //here is the exception: Root element is missing.
       topPanel.Children.Add(obj); 
      }
    


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    • Proposed as answer by TSoftware-Old Saturday, March 17, 2012 2:06 PM
    • Marked as answer by Kee Poppy Wednesday, March 21, 2012 2:01 AM
    Sunday, March 11, 2012 11:25 PM

All replies

  • Are you looking to persist the user interface or the data that is contained within?  If you are trying to save the data, you may want to consider serialization.

    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    Sunday, March 11, 2012 9:24 PM
  • I want to persist UI. The error was due to the fact that when I store the StackPanel MainPanel, it is stored as fragement not complete doc, with the root element. So I just set ConformanceLevel.Fragment. That worked. But now loading from fragment is returning Null. That is obj is null suring debugging.
    Sunday, March 11, 2012 9:29 PM
  • This would appear to work:

      private void ShowXAML(object sender, RoutedEventArgs e)
      {
       TextWriter writer = File.CreateText("xamlfile.xaml");
       //StringBuilder outstr=new StringBuilder();
       //XamlCode.Clear();
       topPanel.Children.Clear();
    
       //this code need for right XML fomating 
       //XmlWriterSettings settings = new XmlWriterSettings();
       //settings.Indent = true;
       //settings.OmitXmlDeclaration = true;
       //XamlDesignerSerializationManager dsm = new XamlDesignerSerializationManager(XmlWriter.Create(writer, settings));
       //this string need for turning on expression saving mode 
       //dsm.XamlWriterMode = XamlWriterMode.Expression;
    
       //XamlWriter.Save(MainPanel, dsm);
       writer.Write(XamlWriter.Save(MainPanel));
    
       writer.Close();
       TextReader reader = File.OpenText("xamlfile.xaml");
       //string outstr = reader.ReadToEnd();
       //XamlCode.Text = outstr;
    
       //XmlReaderSettings xrs = new XmlReaderSettings();
       //xrs.ValidationType = ValidationType.None;
       //XmlReader xmlReader = XmlReader.Create(reader, xrs);
       XmlReader xmlReader = XmlReader.Create(reader);
    
       UIElement obj = (UIElement)XamlReader.Load(xmlReader);  //here is the exception: Root element is missing.
       topPanel.Children.Add(obj); 
      }
    


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    • Proposed as answer by TSoftware-Old Saturday, March 17, 2012 2:06 PM
    • Marked as answer by Kee Poppy Wednesday, March 21, 2012 2:01 AM
    Sunday, March 11, 2012 10:11 PM
  • Or, ot preserve your XamlDesignerSerilizationManager settings:

      private void ShowXAML(object sender, RoutedEventArgs e)
      {
       TextWriter writer = File.CreateText("xamlfile.xaml");
       //StringBuilder outstr=new StringBuilder();
       //XamlCode.Clear();
       topPanel.Children.Clear();
    
       //this code need for right XML fomating 
       //XmlWriterSettings settings = new XmlWriterSettings();
       //settings.Indent = true;
       //settings.OmitXmlDeclaration = true;
       //XamlDesignerSerializationManager dsm = new XamlDesignerSerializationManager(XmlWriter.Create(writer, settings));
       XamlDesignerSerializationManager dsm = new XamlDesignerSerializationManager(XmlWriter.Create(writer));
       //this string need for turning on expression saving mode 
       dsm.XamlWriterMode = XamlWriterMode.Expression;
    
       XamlWriter.Save(MainPanel, dsm);
       //writer.Write(XamlWriter.Save(MainPanel,));
    
       writer.Close();
       TextReader reader = File.OpenText("xamlfile.xaml");
       //string outstr = reader.ReadToEnd();
       //XamlCode.Text = outstr;
    
       //XmlReaderSettings xrs = new XmlReaderSettings();
       //xrs.ValidationType = ValidationType.None;
       //XmlReader xmlReader = XmlReader.Create(reader, xrs);
       XmlReader xmlReader = XmlReader.Create(reader);
    
       UIElement obj = (UIElement)XamlReader.Load(xmlReader);  //here is the exception: Root element is missing.
       topPanel.Children.Add(obj); 
      }
    


    It would be greatly appreciated if you would mark any helpful entries as helpful and if the entry answers your question, please mark it with the Answer link.

    • Proposed as answer by TSoftware-Old Saturday, March 17, 2012 2:06 PM
    • Marked as answer by Kee Poppy Wednesday, March 21, 2012 2:01 AM
    Sunday, March 11, 2012 11:25 PM