?
Solved

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

Posted on 2008-10-09
5
Medium Priority
?
383 Views
Last Modified: 2013-12-17
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.
0
Comment
Question by:kmitch62
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 7

Expert Comment

by:technofile
ID: 22682596
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
 
LVL 47

Accepted Solution

by:
Wayne Taylor (webtubbs) earned 1000 total points
ID: 22682628
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
 
LVL 3

Expert Comment

by:trunghieubkit
ID: 22684253
@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
 
LVL 3

Assisted Solution

by:trunghieubkit
trunghieubkit earned 1000 total points
ID: 22684306
@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
 

Author Comment

by:kmitch62
ID: 22687755
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

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

719 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question