ERROR saving Grid as XML

F

Filippo Pandiani

SCENARIO
=======================================
I have a Grid and I want to save the values on an XML.
Let me say that I am NOT using DataSet to load valus on my grid.

PROBLEM
=======================================
When I am saving a Grid to XML I get the following exception:
"Token StartElement in state Epilog would result in an invalid XML
document"


SOURCE CODE
=======================================
myXmlTextWriter = new XmlTextWriter (aFileName, null);

myXmlTextWriter.Formatting = Formatting.Indented;
myXmlTextWriter.WriteStartDocument();

myXmlTextWriter.WriteStartElement("MyRoot");

for (int i=0; i<this.MyDataGrid.Rows.Count; i++)
{
myXmlTextWriter.WriteStartElement("MyTag");

for (int x=0; x<this.MyDataGrid.Columns.Count; x++)
{

myXmlTextWriter.WriteElementString(this.MyDataGrid.Columns[x].HeaderText.Tri
m(), this.MyDataGrid.Rows.Cells[x].Text);
}

myXmlTextWriter.WriteEndElement();
myXmlTextWriter.Flush();
}

myXmlTextWriter.WriteEndElement();

myXmlTextWriter.Flush();
myXmlTextWriter.Close();
=======================================

Anybody sees anything wrong ??


Thanks,
Filippo.
 
F

Filippo Pandiani

It seems that I cannot repeat the call
myXmlTextWriter.WriteStartElement("MyTag");
more than once.

So if that is the problem, how can I write an XML as:
<MyRoot>
<MyTag>
<bubu>111</bubu>
<baba>222</baba>
</MyTag>

<MyTag>
<bubu>333</bubu>
<baba>444</baba>
</MyTag>

<MyTag>
<bubu>555</bubu>
<baba>666</baba>
</MyTag>
</MyRoot>

......with multiple "MyTag" tags?

I am currently using an XmlTextWriter, could be this the problem?

Thanks,
Filippo.
 
F

Frank Oquendo

Filippo said:
It seems that I cannot repeat the call
myXmlTextWriter.WriteStartElement("MyTag");
more than once.

Whatever the problem is, that's not it.

--
There are 10 kinds of people. Those who understand binary and those who
don't.

http://code.acadx.com
(Pull the pin to reply)
 
F

Filippo Pandiani

OK,
I have found the reason why the error was occurring and it was my mistake.

I had an IF statement when I was creating the <MyRoot> node and for one
specific file, the IF statement was not creating the Root. ..something like:
if (filetype !=1)
myXmlTextWriter.WriteStartElement("MyRoot");

Therefore on that specific scenario, I was setting the repeated node "MyTag"
at the root level,
therefore attempting to create an XML as:

<MyTag>
<bubu>111</bubu>
<baba>222</baba>
</MyTag>
<MyTag>
<bubu>333</bubu>
<baba>444</baba>
</MyTag>
<MyTag>
<bubu>555</bubu>
<baba>666</baba>
</MyTag>


....which is invalid cause it has un undefined number of Root nodes called
"MyTag".

My mistake ...or let's call it my BUG !!!

Filippo.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top