Solved

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

Posted on 2008-10-09
5
373 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
5 Comments
 
LVL 7

Expert Comment

by:technofile
Comment Utility
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 250 total points
Comment Utility
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
Comment Utility
@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 250 total points
Comment Utility
@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
Comment Utility
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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Recently while returning home from work my wife (another .NET developer) was murmuring something. On further poking she said that she has been assigned a task where she has to serialize and deserialize objects and she is afraid of serialization. Wha…
Browsing the questions asked to the Experts of this forum, you will be amazed to see how many times people are headaching about monster regular expressions (regex) to select that specific part of some HTML or XML file they want to extract. The examp…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now