xml file write fail???

Discussion in 'ASP .Net Web Controls' started by haiwen, Jul 17, 2003.

  1. haiwen

    haiwen Guest

    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
     
    haiwen, Jul 17, 2003
    #1
    1. Advertising

  2. 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" <>
    !Sender: "haiwen" <>
    !Subject: xml file write fail???
    !Date: Thu, 17 Jul 2003 14:26:26 -0700
    !Lines: 54
    !Message-ID: <03fc01c34caa$146f5de0$>
    !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
    !
    !
     
    Yan-Hong Huang[MSFT], Jul 21, 2003
    #2
    1. Advertising

  3. 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" <>
    !Sender: "haiwen" <>
    !References: <03fc01c34caa$146f5de0$> <#>
    !Subject: RE: xml file write fail???
    !Date: Mon, 21 Jul 2003 07:28:09 -0700
    !Lines: 190
    !Message-ID: <093d01c34f94$4eb33f70$>
    !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" <>
    !>!Sender: "haiwen" <>
    !>!Subject: xml file write fail???
    !>!Date: Thu, 17 Jul 2003 14:26:26 -0700
    !>!Lines: 54
    !>!Message-ID: <03fc01c34caa$146f5de0$>
    !>!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
    !>!
    !>!
    !>
    !>
    !>.
    !>
    !
     
    Yan-Hong Huang[MSFT], Jul 22, 2003
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gordon Beaton
    Replies:
    2
    Views:
    759
    Gordon Beaton
    Sep 20, 2003
  2. Wenjie

    if (f() != FAIL) or if (FAIL != f())?

    Wenjie, Jul 28, 2003, in forum: C Programming
    Replies:
    3
    Views:
    471
    E. Robert Tisdale
    Jul 31, 2003
  3. Replies:
    6
    Views:
    681
  4. Martin Honnen
    Replies:
    0
    Views:
    715
    Martin Honnen
    Nov 13, 2009
  5. Replies:
    5
    Views:
    98
    Chris Angelico
    May 14, 2014
Loading...

Share This Page