none
Dataset.WriteXML table order question RRS feed

  • Question

  • Hi.

    Is there a way to order tables in the XML output by Dataset.WriteXML.

    For eg: If I have a parent tabe Table1 and 2 child tables Child1 and Child2, is there a way to specify which child will appear first in the nesting. This order will be fixed all the time. But it seems like everytime I auto-generate the xsd file, the  child tables are not necessarily nested in the same order.

    Thanks.

    Friday, July 24, 2015 4:26 PM

Answers

  • OK I think I have found a solution.

    It seems like the the child tables get written in the XML in the order their relation with the parent is added in the Relations collection of the dataset. One would have thought that this order would be the order in which you added the relations in the designer. But it doesnt seem so. Or may be the autogenerated code changes things around when you move tables in the designer.

    So if you change the order of the relations, the order in the XML output changes. So to achieve this, I overrode the dataset's constructor and changed the order of the parent-child table relations by first removing the relations and then adding them back. But be very cautious while doing this. The base constructor of the dataset fires an event based on schema changes. Any changes to the Table or Relations collections is considered a schema change and this has the potential of removing relations that should have been there.

    So before I updated the Relations collection, I first removed this event and then added it back after adjusting the order of the Relations.

    So my autogenerated constructor looks like this :

    [global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
    [global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")]
    public MyXMLExportDataSet() {
         this.BeginInit();
         this.InitClass();
         global::System.ComponentModel.CollectionChangeEventHandler schemaChangedHandler = new global::System.ComponentModel.CollectionChangeEventHandler(this.SchemaChanged);
         base.Tables.CollectionChanged += schemaChangedHandler;
         base.Relations.CollectionChanged += schemaChangedHandler;
         this.EndInit();
    }

    My overridden constructor looks like this:

    public MyXMLExportDataSet(bool bSetDateTimeMode)
                : this()
    {
        if ( bSetDateTimeMode )
           SetDateTimeMode();
        base.Relations.CollectionChanged -= this.SchemaChanged;
        SetTableOrder();
        base.Relations.CollectionChanged += this.SchemaChanged;
    }

    private void SetTableOrder()
    {
        this.Relations.Remove(relationFK_Parent_VW_Child1);
        this.Relations.Remove(relationFK_Parent_VW_Child2);

        this.Relations.Add(relationFK_Parent_VW_Child2);
        this.Relations.Add(relationFK_Parent_VW_Child1);

    }

    Would love to hear opinions on this solution. Let me know if you see some pitfalls other than those I noted.

    I have been looking to do this for a while and did not find any reasonable solutions by googling. So if you know of a site that achieves this in a better way, please post the site in a reply.

    Thanks.

     

     

     

     

     

    Friday, July 24, 2015 9:27 PM