2

I followed this SO answer to turn the below xml structure into a dataset.

<?xml version="1.0" encoding="UTF-8"?>
    <data>
        <child1>
            <to>alice@mail.com</to>                 
            <from>bob@mail.com</from>
            <name>alice alicia</name>
        </child1>
        <child2>
            <file>
                <size>123</size>
                <content>lorem ipsum</content>
            </file> 
         </child2>
     </data>

It turned into 3 tables in the dataset:

  • child1 table with 3 columns (to,from,name).
  • child2 table with 1 columns (file).
  • file table with 2 columns (size,content).

At the end, I want to have only 1 table with 5 columns (to,from,name,size,content).

Could I use a schema to design the way ReadXml in DataSet interprets the xml into my desired table? And if so, how would such schema look like?

I thought of 2 more solutions, though I less prefer them because they require manual handling, which I believe are costly when handling many xmls:

  1. Changing the data set after reading xml, consolidating the 3 tables into one and removing the unneeded column (file).
  2. Editing the xml manually before calling ReadXml method in DataSet.
bentz123
  • 1,081
  • 7
  • 16
  • 1
    Have you considered to parse your XML to C#? http://xmltocsharp.azurewebsites.net, you will save a lot of time and handle the object easily from your side. Give it a try. – Federico Navarrete May 02 '18 at 09:02
  • You could use XSLT to transform your xml to a flat structure. Also, why have de tags `child1` and `child2` a different name if they are meant to be the same? – Peter Bruins May 02 '18 at 09:02
  • 1
    You could loop through the XML manually and create your own xml-to-table logic for it. You could also use third-party solutions for that as pointed out in the comments of your question. The options you suggest are possible (and simpler), but require that you handle the data multiple times. This is fine for small datasets, but may cause slow processing when you handle large datasets. See on how to loop through XML-files yourself – H.J. Meijer May 02 '18 at 09:55
  • 1
    At the end, I used the approach that @FedericoNavarrete suggested. Worked very well for me along with this SO [answer] (https://stackoverflow.com/questions/10518372/how-to-deserialize-xml-to-object). Thanks! – bentz123 May 06 '18 at 18:15

0 Answers0