7 Replies - 5278 Views - Last Post: 21 May 2012 - 10:49 AM Rate Topic: -----

#1 Samms  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-May 12

ERROR - Data at the root level is invalid.: Line 1, position 1

Posted 20 May 2012 - 11:37 AM

Hi,
I want make a notepad. But I have problem with function Open file. This is my code:
private void MenuItem_Click_Open(object sender, RoutedEventArgs e)
        {
            FlowDocument myFlowDoc = new FlowDocument();

            OpenFileDialog opf = new OpenFileDialog();

            opf.Filter = filtr;

            if (opf.ShowDialog() == true)
            {
                StreamReader sr = new StreamReader(opf.FileName);
                FlowDocument doc = XamlReader.Parse(sr.ReadToEnd()) as FlowDocument;
                richTextBox1.Document = doc;
                this.Title = System.IO.Path.GetFileName(opf.FileName);
                sr.Close();
            }
        }

Problem manifests itself in here:
 FlowDocument doc = XamlReader.Parse(sr.ReadToEnd()) as FlowDocument;

The error message is given in the title this post. Thanks for any help!

Is This A Good Question/Topic? 0
  • +

Replies To: ERROR - Data at the root level is invalid.: Line 1, position 1

#2 Momerath  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1012
  • View blog
  • Posts: 2,444
  • Joined: 04-October 09

Re: ERROR - Data at the root level is invalid.: Line 1, position 1

Posted 20 May 2012 - 11:56 PM

It's telling you that the document you are trying to read isn't valid XAML, in fact the very first character is causing an issue (Line 1, position 1).
Was This Post Helpful? 0
  • +
  • -

#3 Samms  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-May 12

Re: ERROR - Data at the root level is invalid.: Line 1, position 1

Posted 21 May 2012 - 12:59 AM

Thanks - and what should I do?
Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3647
  • View blog
  • Posts: 11,415
  • Joined: 05-May 12

Re: ERROR - Data at the root level is invalid.: Line 1, position 1

Posted 21 May 2012 - 03:24 AM

What are the contents of the file you are choosing in the OpenFile dialog? Perhaps the file does not contain XAML.
Was This Post Helpful? 0
  • +
  • -

#5 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: ERROR - Data at the root level is invalid.: Line 1, position 1

Posted 21 May 2012 - 05:16 AM

1. Notepad doesn't do individual character formatting, so there is no reason to load this into a Flowdocument. Simply load it into a multi-line textbox.

2. You don't want to use the XamlReader, as you are most likely not loading XAML from the file. The XamlParser class is designed to be used why you want to dynamically load a screen's XAML from a file. That's not what you are doing. You can simply use the TextReader class.

Also moving to WPF.
Was This Post Helpful? 0
  • +
  • -

#6 Samms  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-May 12

Re: ERROR - Data at the root level is invalid.: Line 1, position 1

Posted 21 May 2012 - 06:25 AM

And how get string into FlowDocument? - (how convert string to FlowDocument) Thank you!
Was This Post Helpful? 0
  • +
  • -

#7 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1525
  • View blog
  • Posts: 5,961
  • Joined: 21-March 08

Re: ERROR - Data at the root level is invalid.: Line 1, position 1

Posted 21 May 2012 - 10:31 AM

The FlowDocument should be used when you want advanced formatting. Notepad doesn't have hardly any formatting options. If you are trying to build Notepad, don't use the Flowdocument. Simply use a textbox.
Was This Post Helpful? 0
  • +
  • -

#8 Samms  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 20-May 12

Re: ERROR - Data at the root level is invalid.: Line 1, position 1

Posted 21 May 2012 - 10:49 AM

I created ordinary notepad already long ago. But I did not work with WPF - I worked with Windows Form. Now I want make notepad for work with syntax programing languages,... Such as the Notepad++.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1