Sorting XML into a Hierarchy

Discussion in 'XML' started by csharp, Aug 4, 2006.

  1. csharp

    csharp Guest

    I am generating an xml file off an employees table from a database to
    use to databind a treeview to on a master page rather than call the db
    every time the page loads.

    I need to take this XML and sort it hierarchially so when the treeview
    gets it, it shows all employees sorted with child nodes for employees
    who report to them, etc.

    I have never worked with XML but found enough online to assume an xsl
    file would do the trick, provided it was written correctly.

    What I have so far, thats not working, is this.




    CODE TO GENERATE XML FILE FROM DB

    DataSet SQLDataset = new DataSet("Employees");
    SqlDataAdapter DBAdapter = new SqlDataAdapter("Select * FROM
    tm2Employee", dbConn);
    DBAdapter.Fill(SQLDataset);
    SQLDataset.WriteXml(System.AppDomain.CurrentDomain.BaseDirectory +
    "xml.xml", XmlWriteMode.WriteSchema);

    //sort the XML Hierarchically
    XslCompiledTransform xsltransform = new XslCompiledTransform();
    xsltransform.Load(System.AppDomain.CurrentDomain.BaseDirectory +
    "xsl.xsl");
    xsltransform.Transform(System.AppDomain.CurrentDomain.BaseDirectory +
    "xml.xml", System.AppDomain.CurrentDomain.BaseDirectory + "xml2.xml2");


    SAMPLE OF XML FILE GENERATED

    <Employees>
    <Table>
    <LoginID>LEGAL\SmithJS</LoginID>
    <EmployeeName>Smith, John</EmployeeName>
    <EmployeeID>111111</EmployeeID>
    <DOB>1980-07-23T00:00:00-04:00</DOB>
    <AuthLevel>3</AuthLevel>
    <ReportsTo>222222</ReportsTo>
    </Table>
    </Employees>



    XSL FILE

    <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:key name="ReportsTo" match="Employee" use="@ReportsTo"/>
    <xsl:template match="/">
    <Employee>
    <xsl:for-each select="key('ReportsTo','0')">
    <xsl:call-template name="recurse"/>
    </xsl:for-each>
    </Employee>
    </xsl:template>
    <xsl:template name="recurse">
    <xsl:copy>
    <xsl:copy-of select="@*"/>
    <xsl:if test="key('ReportsTo',@id)">
    <Employee>
    <xsl:for-each select="key('ReportsTo',@id)">
    <xsl:call-template name="recurse"/>
    </xsl:for-each>
    </Employee>
    </xsl:if>
    </xsl:copy>
    </xsl:template>
    </xsl:stylesheet>




    ENVIRONMENT

    ASP.net 2.0
    MSSQL Server 2000
    Windows Server (unsure of version)
    csharp, Aug 4, 2006
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Xenia
    Replies:
    4
    Views:
    438
    Xenia
    Nov 25, 2003
  2. Harry Zoroc
    Replies:
    1
    Views:
    934
    Gregory Vaughan
    Jul 12, 2004
  3. Replies:
    7
    Views:
    620
    Joris Gillis
    Dec 10, 2004
  4. Juho Jussila

    Deepening an XML hierarchy

    Juho Jussila, Jul 14, 2005, in forum: XML
    Replies:
    6
    Views:
    561
    Juho Jussila
    Jul 15, 2005
  5. Bostonasian
    Replies:
    8
    Views:
    493
    Peter Flynn
    Feb 15, 2007
Loading...

Share This Page