xml file write fail???

H

haiwen

Hello, everyone:
another strange problem, could anyone help me.

it seems dataset's WriteXml(file) just works well when
this dataset only has one table.

for example:

with kc.xml file:
<?xml version="1.0" standalone="yes"?>
<test>
<question>
<questionID>1</questionID>
<reference>1</reference>
</question>
<question>
<questionID>2</questionID>
<reference>2</reference>
</question>
</test>
below code works well.

string strPath=Server.MapPath(Request.ApplicationPath);
DataSet db=new DataSet();
db.ReadXml(strPath+"\\kc.xml");
DataRow rowUser=db.Tables["question"].NewRow();
db.Tables["question"].Rows.Add(rowUser);
rowUser["questionID"]="99";
rowUser["reference"]="2";
but if I just change the xml file as:
<?xml version="1.0" standalone="yes"?>
<test >
<testName>aaa</testName>
<question>
<questionID>1</questionID>
<reference>1</reference>
</question>
<question>
<questionID>2</questionID>
<reference>2</reference>
</question>
</test>
then the code above doesn't work and i always get:

Exception Details: System.InvalidOperationException: Token
StartElement in state Epilog would result in an invalid
XML document.

I can not figure it out, please help.

Thanks a lot,

haiwen
 
Y

Yan-Hong Huang[MSFT]

Hello Haiwen,

I did reproduce the error as you mentioned. I feel that the format of the new XML file may have some error so that the dataset
loads the wrong table.

So I added code to produce a XML file which contains two tables first to see which format it is. Please refer to the following
codes:

// Create a DataSet with one table and two columns.
DataSet OriginalDataSet = new DataSet("myDataSet");
OriginalDataSet.Namespace= "NetFrameWork";

DataTable myTable1 = new DataTable("myTable1");
DataColumn c11 = new DataColumn("id1", Type.GetType("System.Int32"));
c11.AutoIncrement= true;
DataColumn c12 = new DataColumn("item1");
myTable1.Columns.Add(c11);
myTable1.Columns.Add(c12);
OriginalDataSet.Tables.Add(myTable1);

DataTable myTable2 = new DataTable("myTable2");
DataColumn c21 = new DataColumn("id2", Type.GetType("System.Int32"));
c21.AutoIncrement= true;
DataColumn c22 = new DataColumn("item2");
myTable2.Columns.Add(c21);
myTable2.Columns.Add(c22);
OriginalDataSet.Tables.Add(myTable2);

// Add ten rows.
DataRow newRow;
for(int i = 0; i < 2; i++)
{
newRow = myTable1.NewRow();
newRow["item1"]= "itemaa " + i;
myTable1.Rows.Add(newRow);
}
for(int i = 0; i < 2; i++)
{
newRow = myTable2.NewRow();
newRow["item2"]= "itembb " + i;
myTable2.Rows.Add(newRow);
}

OriginalDataSet.AcceptChanges();

string xmlFilename = "myXmlDocument.xml";
// Create FileStream
System.IO.FileStream fsWriteXml = new System.IO.FileStream
(xmlFilename, System.IO.FileMode.Create);
// Create an XmlTextWriter to write the file.
System.Xml.XmlTextWriter xmlWriter = new System.Xml.XmlTextWriter
(fsWriteXml, System.Text.Encoding.Unicode);
// Use WriteXml to write the document.
OriginalDataSet.WriteXml(xmlWriter);
// Close the FileStream.
fsWriteXml.Close();

After that, I loaded this new XML file and did exactly the same thing as your codes. Now it works fine to add a new record. So I
think you need to refer to the produced xml file for the format.

Thanks.

Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
!Content-Class: urn:content-classes:message
!From: "haiwen" <[email protected]>
!Sender: "haiwen" <[email protected]>
!Subject: xml file write fail???
!Date: Thu, 17 Jul 2003 14:26:26 -0700
!Lines: 54
!Message-ID: <[email protected]>
!MIME-Version: 1.0
!Content-Type: text/plain;
! charset="iso-8859-1"
!Content-Transfer-Encoding: 7bit
!X-Newsreader: Microsoft CDO for Windows 2000
!Thread-Index: AcNMqhRvR6iw7PzdRViA3rT7ZpGjNg==
!X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
!Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
!Path: cpmsftngxa06.phx.gbl
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet.webcontrols:13224
!NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
!X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
!
!Hello, everyone:
!another strange problem, could anyone help me.
!
!it seems dataset's WriteXml(file) just works well when
!this dataset only has one table.
!
!for example:
!
!with kc.xml file:
!<?xml version="1.0" standalone="yes"?>
!<test>
! <question>
! <questionID>1</questionID>
! <reference>1</reference>
! </question>
! <question>
! <questionID>2</questionID>
! <reference>2</reference>
! </question>
!</test>
!below code works well.
!
!string strPath=Server.MapPath(Request.ApplicationPath);
!DataSet db=new DataSet();
!db.ReadXml(strPath+"\\kc.xml");
!DataRow rowUser=db.Tables["question"].NewRow();
!db.Tables["question"].Rows.Add(rowUser);
!rowUser["questionID"]="99";
!rowUser["reference"]="2";
!but if I just change the xml file as:
!<?xml version="1.0" standalone="yes"?>
!<test >
! <testName>aaa</testName>
! <question>
! <questionID>1</questionID>
! <reference>1</reference>
! </question>
! <question>
! <questionID>2</questionID>
! <reference>2</reference>
! </question>
!</test>
!then the code above doesn't work and i always get:
!
!Exception Details: System.InvalidOperationException: Token
!StartElement in state Epilog would result in an invalid
!XML document.
!
!I can not figure it out, please help.
!
!Thanks a lot,
!
!haiwen
!
!
 
Y

Yan-Hong Huang[MSFT]

Hello Haiwen,

I am glad to hear it. How about that problem on setting primary key in dataset tables? Thanks.


Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
!Content-Class: urn:content-classes:message
!From: "haiwen" <[email protected]>
!Sender: "haiwen" <[email protected]>
!References: <[email protected]> <#[email protected]>
!Subject: RE: xml file write fail???
!Date: Mon, 21 Jul 2003 07:28:09 -0700
!Lines: 190
!Message-ID: <[email protected]>
!MIME-Version: 1.0
!Content-Type: text/plain;
! charset="iso-8859-1"
!Content-Transfer-Encoding: 7bit
!X-Newsreader: Microsoft CDO for Windows 2000
!Thread-Index: AcNPlE6wjK+YuTccT1KaBwA4SsI5+Q==
!X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
!Newsgroups: microsoft.public.dotnet.framework.aspnet.webcontrols
!Path: cpmsftngxa06.phx.gbl
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet.webcontrols:13293
!NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
!X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.webcontrols
!
!Hello Yanhong Huang:
!
!Thank you so much for your help. It works well now,
!thanks again,
!
!haiwen
!
!>-----Original Message-----
!>Hello Haiwen,
!>
!>I did reproduce the error as you mentioned. I feel that
!the format of the new XML file may have some error so that
!the dataset
!>loads the wrong table.
!>
!>So I added code to produce a XML file which contains two
!tables first to see which format it is. Please refer to
!the following
!>codes:
!>
!> // Create a DataSet with one table
!and two columns.
!> DataSet OriginalDataSet = new
!DataSet("myDataSet");
!>
! OriginalDataSet.Namespace= "NetFrameWork";
!>
!> DataTable myTable1 = new DataTable
!("myTable1");
!> DataColumn c11 = new DataColumn
!("id1", Type.GetType("System.Int32"));
!> c11.AutoIncrement= true;
!> DataColumn c12 = new DataColumn
!("item1");
!> myTable1.Columns.Add(c11);
!> myTable1.Columns.Add(c12);
!> OriginalDataSet.Tables.Add
!(myTable1);
!>
!> DataTable myTable2 = new DataTable
!("myTable2");
!> DataColumn c21 = new DataColumn
!("id2", Type.GetType("System.Int32"));
!> c21.AutoIncrement= true;
!> DataColumn c22 = new DataColumn
!("item2");
!> myTable2.Columns.Add(c21);
!> myTable2.Columns.Add(c22);
!> OriginalDataSet.Tables.Add
!(myTable2);
!>
!> // Add ten rows.
!> DataRow newRow;
!> for(int i = 0; i < 2; i++)
!> {
!> newRow = myTable1.NewRow();
!> newRow["item1"]= "itemaa "
!+ i;
!> myTable1.Rows.Add(newRow);
!> }
!> for(int i = 0; i < 2; i++)
!> {
!> newRow = myTable2.NewRow();
!> newRow["item2"]= "itembb "
!+ i;
!> myTable2.Rows.Add(newRow);
!> }
!>
!> OriginalDataSet.AcceptChanges();
!>
!> string xmlFilename
!= "myXmlDocument.xml";
!> // Create FileStream
!> System.IO.FileStream fsWriteXml =
!new System.IO.FileStream
!> (xmlFilename,
!System.IO.FileMode.Create);
!> // Create an XmlTextWriter to
!write the file.
!> System.Xml.XmlTextWriter xmlWriter
!= new System.Xml.XmlTextWriter
!> (fsWriteXml,
!System.Text.Encoding.Unicode);
!> // Use WriteXml to write the
!document.
!> OriginalDataSet.WriteXml
!(xmlWriter);
!> // Close the FileStream.
!> fsWriteXml.Close();
!>
!>After that, I loaded this new XML file and did exactly
!the same thing as your codes. Now it works fine to add a
!new record. So I
!>think you need to refer to the produced xml file for the
!format.
!>
!>Thanks.
!>
!>Best regards,
!>Yanhong Huang
!>Microsoft Online Partner Support
!>
!>Get Secure! - www.microsoft.com/security
!>This posting is provided "AS IS" with no warranties, and
!confers no rights.
!>
!>--------------------
!>!Content-Class: urn:content-classes:message
!>!From: "haiwen" <[email protected]>
!>!Sender: "haiwen" <[email protected]>
!>!Subject: xml file write fail???
!>!Date: Thu, 17 Jul 2003 14:26:26 -0700
!>!Lines: 54
!>!Message-ID: <[email protected]>
!>!MIME-Version: 1.0
!>!Content-Type: text/plain;
!>! charset="iso-8859-1"
!>!Content-Transfer-Encoding: 7bit
!>!X-Newsreader: Microsoft CDO for Windows 2000
!>!Thread-Index: AcNMqhRvR6iw7PzdRViA3rT7ZpGjNg==
!>!X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
!>!Newsgroups:
!microsoft.public.dotnet.framework.aspnet.webcontrols
!>!Path: cpmsftngxa06.phx.gbl
!>!Xref: cpmsftngxa06.phx.gbl
!microsoft.public.dotnet.framework.aspnet.webcontrols:13224
!>!NNTP-Posting-Host: TK2MSFTNGXA12 10.40.1.164
!>!X-Tomcat-NG:
!microsoft.public.dotnet.framework.aspnet.webcontrols
!>!
!>!Hello, everyone:
!>!another strange problem, could anyone help me.
!>!
!>!it seems dataset's WriteXml(file) just works well when
!>!this dataset only has one table.
!>!
!>!for example:
!>!
!>!with kc.xml file:
!>!<?xml version="1.0" standalone="yes"?>
!>!<test>
!>! <question>
!>! <questionID>1</questionID>
!>! <reference>1</reference>
!>! </question>
!>! <question>
!>! <questionID>2</questionID>
!>! <reference>2</reference>
!>! </question>
!>!</test>
!>!below code works well.
!>!
!>!string strPath=Server.MapPath(Request.ApplicationPath);
!>!DataSet db=new DataSet();
!>!db.ReadXml(strPath+"\\kc.xml");
!>!DataRow rowUser=db.Tables["question"].NewRow();
!>!db.Tables["question"].Rows.Add(rowUser);
!>!rowUser["questionID"]="99";
!>!rowUser["reference"]="2";
!>!but if I just change the xml file as:
!>!<?xml version="1.0" standalone="yes"?>
!>!<test >
!>! <testName>aaa</testName>
!>! <question>
!>! <questionID>1</questionID>
!>! <reference>1</reference>
!>! </question>
!>! <question>
!>! <questionID>2</questionID>
!>! <reference>2</reference>
!>! </question>
!>!</test>
!>!then the code above doesn't work and i always get:
!>!
!>!Exception Details: System.InvalidOperationException:
!Token
!>!StartElement in state Epilog would result in an invalid
!>!XML document.
!>!
!>!I can not figure it out, please help.
!>!
!>!Thanks a lot,
!>!
!>!haiwen
!>!
!>!
!>
!>
!>.
!>
!
 

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

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top