473,405 Members | 2,338 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

ERROR saving Grid as XML

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[i].Cells[x].Text);
}

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

myXmlTextWriter.WriteEndElement();

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

Anybody sees anything wrong ??
Thanks,
Filippo.


Nov 12 '05 #1
4 2779
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.


"Filippo Pandiani" <pa********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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[i].Cells[x].Text);
}

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

myXmlTextWriter.WriteEndElement();

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

Anybody sees anything wrong ??
Thanks,
Filippo.

Nov 12 '05 #2
That can't be it ... the error message states that your writer is in the
epilog state, i.e. the document is written and you're trying to write a 2nd
root element. Are you re-using the XmlTextWriter after you already wrote a
complete document ? It doesn't look like that in the sample code you posted
though.

Check why your writer thinks you already closed all open elements, including
the root element.

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"Filippo Pandiani" <pa********@hotmail.com> wrote in message
news:uo**************@TK2MSFTNGP11.phx.gbl...
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.


"Filippo Pandiani" <pa********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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[i].Cells[x].Text);
}

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

myXmlTextWriter.WriteEndElement();

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

Anybody sees anything wrong ??
Thanks,
Filippo.


Nov 12 '05 #3
You are right,
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.


"Christoph Schittko [MVP]" <ch********************@austin.rr.com> wrote in
message news:uQ**************@TK2MSFTNGP12.phx.gbl...
That can't be it ... the error message states that your writer is in the
epilog state, i.e. the document is written and you're trying to write a 2nd root element. Are you re-using the XmlTextWriter after you already wrote a
complete document ? It doesn't look like that in the sample code you posted though.

Check why your writer thinks you already closed all open elements, including the root element.

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"Filippo Pandiani" <pa********@hotmail.com> wrote in message
news:uo**************@TK2MSFTNGP11.phx.gbl...
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.


"Filippo Pandiani" <pa********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
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[i].Cells[x].Text);
}

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

myXmlTextWriter.WriteEndElement();

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

Anybody sees anything wrong ??
Thanks,
Filippo.



Nov 12 '05 #4
I'm glad I could help
Have a good weekend,

Christoph Schittko [MVP]
Software Architect, .NET Mentor

"Filippo Pandiani" <pa********@hotmail.com> wrote in message
news:OX**************@TK2MSFTNGP12.phx.gbl...
You are right,
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.


"Christoph Schittko [MVP]" <ch********************@austin.rr.com> wrote in
message news:uQ**************@TK2MSFTNGP12.phx.gbl...
That can't be it ... the error message states that your writer is in the
epilog state, i.e. the document is written and you're trying to write a

2nd
root element. Are you re-using the XmlTextWriter after you already wrote a
complete document ? It doesn't look like that in the sample code you

posted
though.

Check why your writer thinks you already closed all open elements,

including
the root element.

--
HTH
Christoph Schittko [MVP]
Software Architect, .NET Mentor

"Filippo Pandiani" <pa********@hotmail.com> wrote in message
news:uo**************@TK2MSFTNGP11.phx.gbl...
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.


"Filippo Pandiani" <pa********@hotmail.com> wrote in message
news:%2****************@TK2MSFTNGP10.phx.gbl...
> 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[i].Cells[x].Text);
> }
>
> myXmlTextWriter.WriteEndElement();
> myXmlTextWriter.Flush();
> }
>
> myXmlTextWriter.WriteEndElement();
>
> myXmlTextWriter.Flush();
> myXmlTextWriter.Close();
> =======================================
>
> Anybody sees anything wrong ??
>
>
> Thanks,
> Filippo.
>
>
>
>



Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: wayne dooley | last post by:
This should be easy. I have 2 tables - CUST and SUBS, which have a 1 to many relationship. I created a form to show fields from CUST and a subform to show those in SUBS. The 2 tables are related...
3
by: Filippo Pandiani | last post by:
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...
0
by: NCrum | last post by:
I enter a time in a grid either directly like 10:01 or via the Date picker formatted for time the correct time displays in the grid OK but does not save back to the database, todays' date does but...
4
by: John Kandell | last post by:
Hi, I posted this in the asp.net group, but didn't get a response. Maybe someone here can help me with this... --- Would someone be able to shed some light on what is the cost of saving a...
1
by: Douglas Gage | last post by:
I have two columns Question Answers Q1 Q2 Q3 Q4 Submit
1
by: Nikhil Patel | last post by:
Hi, I bind a grid to a DataView object. I lose the reference to the underlying DataTable and the DataSet in a postback. Here is the code. Page_Load works fine and I am able to see the rows in...
11
by: Tom | last post by:
I am planning on adding a Preferences form to my application and using the Property Grid to display the preferences to the user. What do you think would be the best way to save these preferences...
5
by: Steve Marshall | last post by:
Hi all, I am converting an app which used a picturebox to draw graphs etc onto, then saved them to a file. I can certainly draw things onto a picturbox in VB.NET, but how do I save them to a...
0
by: madhu raju | last post by:
iam saving the data into Ms-Access database using Vb.net Application In that application iam using Data grid view to save the data into the database Here in the below code iam saving the name of...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.