How do i use readxml into datatables when there is no xml schema provided

I have several xml files that are being delivered to me from an application web service.  These files have no inline schema and there is no xsd file.  I need to import these xml files into separate tables in a dataset for us in another application.All of the XML documents will have the same structure, but no schema is provided.

Using Dataset.readxml allows me to read a single xml document and it works perfectly.  All subsequent attempts though do not import any data.  How do i create the tables and load the data individually without getting the error like "datatable does not support inferred schema"?

Please help as this solution will be used in several applications for my employer.
kmitch62Asked:
Who is Participating?

Improve company productivity with a Business Account.Sign Up

x
 
Wayne Taylor (webtubbs)Connect With a Mentor Commented:
Try this function, which accepts an array of file paths and returns a dataset....
public DataSet LoadXMLIntoDataSet(string[] files)
{
    
    DataSet ds = new DataSet();
    
    foreach (string file in files) {
        DataSet d = new DataSet();
        d.ReadXml(file);
        ds.Tables.Add(d.Tables(0).Clone);
        d.Dispose();
    }
    
    return ds;
    
}

Open in new window

0
 
technofileCommented:
How complicated are these XML files? Do you know what is supposed to go where?
If you can post your code and a sample of the XML I might be able to help.
0
 
trunghieubkitCommented:
@webtubbs

Yours is not enough,
But the description of clone function of  DataTable is

Clones the structure of the System.Data.DataTable, including all System.Data.DataTable
schemas and constraints.
Returns:        A new System.Data.DataTable with the same schema as the current

Thus,
 ds.Tables has a tables with no data



0
 
trunghieubkitConnect With a Mentor Commented:
@webtubbs


Yours!
public DataSet LoadXMLIntoDataSet(string[] files)
{
    DataSet ds = new DataSet();
    DataSet d = new DataSet();
 
    foreach (string file in files)
    {
        d.ReadXml(file);
 
        DataTable tbl = d.Tables[0];
        d.Tables.Clear();
 
        ds.Tables.Add(tbl);
    }
 
    return ds;
}

Open in new window

0
 
kmitch62Author Commented:
Thanks guys,

Actually both trunghieubkit: and webtubbs: were very close.  trunghieubkit's solution worked best for me with some tiny adjustments.  I needed to assign a tablename because the xml passed in will have the same structure, thus the tables will have the same default tableName.


Hereis the final solution that is working perfectly:
 
        public DataSet LoadXMLIntoDataSet(string[] files)
        {
            DataSet ds = new DataSet();
            DataSet d = new DataSet();
            System.Data.DataTable tbl = new System.Data.DataTable();
            int cntr = 0;
 
            foreach (string file in files)
            {
                d.ReadXml(file);
                tbl = d.Tables[0];
                tbl.TableName = "Table " + cntr;
                d.Tables.Clear();
                ds.Tables.Add(tbl);
                cntr++;
            }
            return ds;
        }

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.