4

I have written the following piece of code that reads through a given xml-file and writes the contents into a data-table. Please do NOT suggest to use LinqToXml as that option is ruled because this is a legacy application.

            // create the DataTable that will hold the data
            DataTable table = new DataTable("ListOfPersonsWithInfo");


            // open the file using a Stream
            using (Stream stream = new FileStream(fileNameWithAbsolutePath, FileMode.Open, FileAccess.Read))
            {
                // create the table with the appropriate column names
                table.Columns.Add("Name", typeof(String));
                table.Columns.Add("ImagePath", typeof(String));
                table.Columns.Add("Address", typeof(String));

                // use ReadXml to read the XML stream
                table.ReadXml(stream);

                // tried with this overload-option as well but didnt help
                //table.ReadXml(fileNameWithAbsolutePath);

                // return the results
                return table;
            }

BUT the returned-table contains ZERO rows...!!! where as the actual xml file has '3 rows' and is structured as follows (ANY IDEA what is going wrong here?):

<?xml version="1.0" encoding="utf-8"?>
<Details>
    <EachPerson>
        <Name>Jack</Name>
        <ImagePathAndFileName>C:\Users\Public\Pictures\Sample Pictures\Desert.jpg</ImagePathAndFileName>
        <Address>NewYork</Address>
    </EachPerson>
    <EachPerson>
        <Name>Tom</Name>
        <ImagePathAndFileName>C:\Users\Public\Pictures\Sample Pictures\Desert.jpg</ImagePathAndFileName>
        <Address>London</Address>
    </EachPerson>
    <EachPerson>
        <Name>Jill</Name>
        <ImagePathAndFileName>C:\Users\Public\Pictures\Sample Pictures\Desert.jpg</ImagePathAndFileName>
        <Address>Tokyo</Address>
    </EachPerson>
</Details>
0

1 Answer 1

11

You can use ReadXML

        DataSet ds = new DataSet();
        ds.ReadXml(fileNameWithAbsolutePath);
        return ds.Tables[0];
Sign up to request clarification or add additional context in comments.

3 Comments

Thats it PraVn... but still unable to fathom as to why the dataTABLE.ReadXml() fails but the dataSET.ReadXml() works?... so is there something specific to internal implementation of this ReadXml.
Yes.<Details> is the root which has three levels which is not possible to load in datatable
Thanks PraVn... u seem to be having a very clear view of the internal workings of ADO.NET. Just in case if it suits you, can you have a look at my other question <stackoverflow.com/questions/9687640/…>. This question nobody has attempted to answer or may be it is too vague for some or could be too time consuming for someone to answer with a diagram).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.