0

Error: System.InvalidOperationException: 'Error in the document XML (0, 0).' Inner Exception

Hello, I have an error in my code, i want to deserialize different XML Files and open it in textboxes, the problem i had is when it takes the file path, i Think that there is where the error is, because it takes a one exact file path(ex.): FileStream FS = new FileStream("example.analy", FileMode.Open, FileAccess.Read); But i replaced it with this Hide Copy Code FileStream FS = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read); To read the files that were selected in the openfiledialog.

3rd Jun 2018, 7:51 PM
SebGM2018
SebGM2018 - avatar
8 Antworten
0
This is my complete code: if (openFileDialog1.ShowDialog() == DialogResult.OK) { if (File.Exists(openFileDialog1.FileName)) { if ((openFileDialog1.OpenFile()) != null) { if (openFileDialog1.FileName != null) { openFileDialog1.Filter = "Analytica Files | *.analy"; XmlSerializer XS = new XmlSerializer(typeof(Reminders.Information)); FileStream FS = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read); Information Info = (Information)XS.Deserialize(FS); XS.Deserialize(FS); Settings.Default.Counter++; Settings.Default.Save(); The problem is that when I try to open a file, appears the next error: System.InvalidOperationException: 'Error en el documento XML (0, 0).' Inner Exception XmlException: Missing Root Element.
3rd Jun 2018, 7:52 PM
SebGM2018
SebGM2018 - avatar
0
Can you post the xml ? Which line causes the exception ?
5th Jun 2018, 8:17 PM
sneeze
sneeze - avatar
0
<?xml version="1.0" encoding="utf-8"?> <Information xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <Title1>My age has never made me wise</Title1> <Description2>Description</Description2> <Date3>2018-02-27T22:05:39</Date3> <Hour4>2018-02-27T22:05:39</Hour4> </Information>
6th Jun 2018, 1:04 AM
SebGM2018
SebGM2018 - avatar
0
Hi Seb, Is your question still open ? I like to test it but I also need. The object definition of "Reminders" and "Information" The XML looks alright.
9th Jun 2018, 9:30 PM
sneeze
sneeze - avatar
0
Do you mean the class code?
10th Jun 2018, 1:51 AM
SebGM2018
SebGM2018 - avatar
0
Yes
10th Jun 2018, 6:05 AM
sneeze
sneeze - avatar
0
You are Deserializing twice. The first Deserialize does everything you want. And works well. After that the position of stream is at the end. The second Deserialize says it cannot find the root-element because it starts reading at the wrong position. try { openFileDialog1.Filter = "Analytica Files | *.analy"; XmlSerializer XS = new XmlSerializer(typeof(Information)); FileStream FS = new FileStream(openFileDialog1.FileName, FileMode.Open, FileAccess.Read); Information Info = (Information)XS.Deserialize(FS); //XS.Deserialize(FS); //Settings.Default.Counter++; Settings.Default.Save(); MessageBox.Show(FS.ToString()); MessageBox.Show(FS.Length.ToString()); MessageBox.Show(Info.Title1); } catch(Exception ex) { MessageBox.Show(ex.Message); MessageBox.Show(ex.InnerException.ToString()); } Add FS.Postion = 0; Between the two Deserialize commands. And the second one does not cause a exception anymore. Information Info = (Information)XS.Deserialize(FS); FS.Position = 0; XS.Deserialize(FS);
10th Jun 2018, 9:24 PM
sneeze
sneeze - avatar
0
Thanks, but the problem is that I need to Serialize Different XML Files And load them, like in this video https://youtu.be/ecvunlsNm9k But Replacing The Var Input With The Information Class In The XML File, Also serializing files (like two different) in one form (at loading it)
11th Jun 2018, 3:40 AM
SebGM2018
SebGM2018 - avatar