none
DataSet.ReadXml – how to control parser order? RRS feed

  • Question

  • Hi

      I use method DataSet.ReadXml where I pass a stream to xml file downloaded from some web page.

      Let assume that I have 2 tables in data set: t1 and t2.

      In xml file table t2 is before t1 but method ReadXml first inserts data to t1 next to t2. Can I define that    updates in data set should be in the same order as data in xml file?

    Regards


    kicaj

    Tuesday, June 12, 2012 1:49 PM

Answers

  • If you can use InferSchema mode when you use ReadXML then you can reserve the indexes.

    Example

    My XML sample

    <bb>44</bb>
    <bb>55</bb>
    <bb>66</bb>
    <aa>11</aa>
    <aa>22</aa>
    <aa>33</aa>

    And my reserve tables will be

    DataSet ds = new DataSet();
    ds.Tables.Add("aa");
    ds.Tables.Add("bb");
    ds.ReadXml(xml,XmlReadMode.InferSchema);

    So all "aa" data will bind to table index 0 in my dataset.

    Hope this helps.


    • Proposed as answer by TinMgAye Tuesday, June 12, 2012 5:36 PM
    • Edited by TinMgAye Wednesday, June 13, 2012 4:30 AM
    • Marked as answer by kicaj Friday, June 15, 2012 8:29 AM
    Tuesday, June 12, 2012 5:34 PM

All replies

  • A quick way is;

    At first, read xml into raw DataSet then clone datatables to anothe DataSet' table. Then you will have control over order now.

    DataSet dsRaw = new DataSet();
    dsRawd.ReadXml(xml);
    
    DataSet dsReal = new DataSet();
    DataTable dt1 = dsReal.Tables.Add("table1");
    dt1 = dsRaw.Tables[1].Clone();

    Don't forget to check null and array index before you clone the datatable.
    Tuesday, June 12, 2012 3:13 PM
  • I do not want use “clone” method because I have a lot of data and problems with memory allocation.

    I want read this xml only one time.

    Regards


    kicaj

    Tuesday, June 12, 2012 3:18 PM
  • If you can use InferSchema mode when you use ReadXML then you can reserve the indexes.

    Example

    My XML sample

    <bb>44</bb>
    <bb>55</bb>
    <bb>66</bb>
    <aa>11</aa>
    <aa>22</aa>
    <aa>33</aa>

    And my reserve tables will be

    DataSet ds = new DataSet();
    ds.Tables.Add("aa");
    ds.Tables.Add("bb");
    ds.ReadXml(xml,XmlReadMode.InferSchema);

    So all "aa" data will bind to table index 0 in my dataset.

    Hope this helps.


    • Proposed as answer by TinMgAye Tuesday, June 12, 2012 5:36 PM
    • Edited by TinMgAye Wednesday, June 13, 2012 4:30 AM
    • Marked as answer by kicaj Friday, June 15, 2012 8:29 AM
    Tuesday, June 12, 2012 5:34 PM