none
DataTable.ReadXml(filename) throws an error. Why?

    Question

  • Visual Basic 2005 running on .NET 2.0 happily reads an XML file into a DataSet, but throws an error when asked to read it into a DataTable, in spite of the ReadXml() documentation which suggests that the DataTable operation is also supported.

    Test.xml is of this form:

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <fruits>
      <fruit>
        <name>apple</name>
        <color>red</color>
        <texture>crunchy</texture>
      </fruit>
      <fruit>
        <name>orange</name>
        <color>orange</color>
        <texture>juicy</texture>
      </fruit>
    <fruits>

    The code:

    Dim fn As String = "C:\Test.xml"

    Dim dts As New DataSet

    dts.ReadXml(fn)  '    This works fine.

    Dim dtb As New DataTable

    dtb.ReadXml(fn) '   Throws an error here

    The error message: "DataTable does not support schema inference from Xml."

    Does anyone know the cause of this error, or how it can be worked around?

     

    • Moved by VMazurModerator Thursday, September 10, 2009 10:17 AM (From:ADO.NET Data Providers)
    Tuesday, January 02, 2007 2:16 PM

Answers

  • You should be able to use ReadXml on the DataTable instance if you first call ReadXmlSchema to pull in a schema describing the data.
    Tuesday, January 02, 2007 3:55 PM
  • try to set the tabel name like:

    Dim ds As New DataTable
    ds.TableName = "TableName"

    ds.ReadXmlSchema("e:\schema.xsd")

    ds.ReadXml("e:\schema.xml")


    Thursday, April 24, 2008 11:18 AM

All replies

  • You should be able to use ReadXml on the DataTable instance if you first call ReadXmlSchema to pull in a schema describing the data.
    Tuesday, January 02, 2007 3:55 PM
  • hi, i have the same problem but i dont know how to make this xmlschema

    could someone create a example for me based in this example?

    <?xml version="1.0" encoding="ISO-8859-1"?>
    <fruits>
      <fruit>
        <name>apple</name>
        <color>red</color>
        <texture>crunchy</texture>
      </fruit>
      <fruit>
        <name>orange</name>
        <color>orange</color>
        <texture>juicy</texture>
      </fruit>
    <fruits>

     

    thanks for help,

    Nilie

    • Proposed as answer by Sameer Narkar Thursday, September 10, 2009 6:35 AM
    Wednesday, March 14, 2007 8:42 PM
  • Sir,

    Nice to be a member of MSDN Forum.

    I would like to say even though i have provided xml schema as described below

    Dim ds As New DataTable

    ds.ReadXmlSchema("e:\schema.xsd")

    ds.ReadXml("e:\schema.xml")

    It throws error...

    DataTable does not support schema inference from Xml.

     

    Friday, March 23, 2007 12:18 PM
  • You can use the DataSet instead of the DataTable.  It doesn't require setting the schema first.

     

    Friday, March 21, 2008 5:50 PM
  • try to set the tabel name like:

    Dim ds As New DataTable
    ds.TableName = "TableName"

    ds.ReadXmlSchema("e:\schema.xsd")

    ds.ReadXml("e:\schema.xml")


    Thursday, April 24, 2008 11:18 AM
  • private

     

     

    {

    DataTable ReturnDataTable()

     

    DataSet

     

     

     

     

    ds.ReadXml(

     

     

    dt = ds.Tables[0].Copy();

     

    }

    XmlFilePath.xml);

     

    DataTable dt = new DataTable(); return dt;
     
    ds = new DataSet();
    • Edited by Sameer Narkar Thursday, September 10, 2009 6:39 AM formatting
    Thursday, September 10, 2009 6:37 AM
  • How about this solution?

    Read the XML into your DataSet.

    load the DataSet into a DataTable using DataTable = DataSet.Tables(0)

    Now you have a DataTble you can work with.

    Wednesday, April 07, 2010 9:53 PM
  • If you have Visual Studio, you can open the XML file into Visual Studio (2010).   

    Under the XML menu, select Create Schema

    Cut and paste the code into a text file using notepad or whatever and rename the file from TXT to XSD and your are done! 

    Tuesday, June 21, 2011 8:10 PM
  • To get around this issue, I got creative with some code:

    // This method adds an XML file from a DataTable.WriteXML()
    // to ds, a DataSet in the parent class.
    void AddTableToDS( String fileName, String tableName )
    {
      DataSet fakeDS = new DataSet();
      DataTable dt = null;
    
      fakeDS.ReadXml( fileName );
    
      dt = fakeDS.Tables[0].Copy();
      dt.TableName = tableName;
      ds.Tables.Add( dt );
    }
    

    This could be easily modified to suit the OP's needs.

    Monday, August 29, 2011 4:38 PM
  • You dont need to create a dataset :

    When declaring the DataTable, give it the name of the xsd element like:

     

     

    DataTable myDataTable = new DataTable ("MyRootElement");

    then continue your code with:

     

    myDataTable.ReadXmlSchema("schema.xsd");

    myDataTable.ReadXml("schema.xml");

     

     


    Web Developper
    Monday, September 19, 2011 3:31 PM