Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Please HELP - DataSet.WriteXml Error !!!!

Status
Not open for further replies.

jamsoft

Programmer
Feb 4, 2004
1
CZ
Hi to everyone here.

If I use DataSet.WriteXml(fileName) I get following error:

"An unhandled exception of type 'System.InvalidOperationException' occurred in system.xml.dll

Additional information: Token StartElement in state Epilog would result in an invalid XML document."

If I comment the last line of following code (ds.WriteXml(fName);) everything is O.K. All data are in dataGrid.
If I try to write modified DataSet back to Xml file I get this error.

The main part of my code:

public Form1()
{

InitializeComponent();

string fName="myFile.xml";

DataSet ds= new DataSet();
DataTable tb;
DataRow rw;

ds.ReadXml(fName);

dataGrid1.DataSource=ds;
dataGrid1.DataMember="data";

tb=ds.Tables["data"];


rw=tb.NewRow();
rw[0]="blablabla";
rw[1]="oooooo";
rw[2]="coucou";

tb.Rows.Add(rw);

ds.WriteXml(fName);


}

Where is the problem ?

Lot of thanks for every answer....

J&M
 
The above code is working but the error that you got is because the structure of your xml file that first you load, next you access a DataTable object named "data" which must have minimum 3 columns because you try to add new row with 3 columns.
Reading about code, you expect to have an myfile.xml file something like that:
Code:
<logon>
  <data key=&quot;locked&quot; value=&quot;false&quot; defaultVal=&quot;&quot; />
  <data key=&quot;Server&quot; value=&quot;atwc6r&quot; defaultVal=&quot;&quot; />
  <data key=&quot;Database&quot; value=&quot;imf&quot; defaultVal=&quot;&quot; />
  <data key=&quot;UserID&quot; value=&quot;client&quot; defaultVal=&quot;&quot; />
  <data2 key=&quot;locked&quot; value=&quot;false&quot; />
  <data2 key=&quot;Server&quot; value=&quot;atwc6r&quot; />
  <data2 key=&quot;Database&quot; value=&quot;imf&quot; />
  <data2 key=&quot;UserID&quot; value=&quot;client&quot; />
</logon>
Reading it your DataSet will contains two tables &quot;data&quot; with 3 columns named &quot;key&quot;, &quot;value&quot; and &quot;defaultVal&quot; while &quot;data2&quot; table have only two columns.
You can query at run time the number of columns, the name of columns and you can add columns also at run time.
Your code is running well with the above myfile.xml and the result will be :
<?xml version=&quot;1.0&quot; standalone=&quot;yes&quot;?>
<logon>
<data key=&quot;locked&quot; value=&quot;false&quot; defaultVal=&quot;&quot; />
<data key=&quot;Server&quot; value=&quot;atwc6r&quot; defaultVal=&quot;&quot; />
<data key=&quot;Database&quot; value=&quot;imf&quot; defaultVal=&quot;&quot; />
<data key=&quot;UserID&quot; value=&quot;client&quot; defaultVal=&quot;&quot; />
<data key=&quot;blablabla&quot; value=&quot;oooooo&quot; defaultVal=&quot;coucou&quot; />
<data2 key=&quot;locked&quot; value=&quot;false&quot; />
<data2 key=&quot;Server&quot; value=&quot;atwc6r&quot; />
<data2 key=&quot;Database&quot; value=&quot;imf&quot; />
<data2 key=&quot;UserID&quot; value=&quot;client&quot; />
</logon>
[/code]
You can see your row added under &quot;data&quot; tag.

I hope this help you to figure more about out how to work with DataSet and xml.
-obislavu-


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top