sorting xml data in server control

Discussion in 'ASP .Net' started by Jon Paal, Feb 21, 2006.

  1. Jon   Paal

    Jon Paal Guest

    I am passing an xml data file to a server control and need to sort the data. The data has three levels(tables).

    What is the recommended approach to do the sorting ?

    (using vb.net and asp.net 2.0)
    Jon Paal, Feb 21, 2006
    #1
    1. Advertising

  2. Jon   Paal

    sloan Guest

    When I have to deal with alot of sorting.... I usually put everything in a
    Strongly Typed DataSet.

    If you have have control over the XML being sent... you can send it as
    xml... to mimick the DataSet schema.

    ...

    If not, you may have to do a Xml to Xml transformation .. to get the xml
    formatted correctly.

    MyStronglyTypedDataSet ds = new MyStrongTypedDataSet();

    ds.//Code here to get your xml into the DataSet

    ds.Select("", "LastName, FirstName, Age Desc");

    the first parameter is the filter... which you set to empty string... the
    second parameter is the sortby.

    Look up the DataSet.Select command.

    xsl sorting is pretty ... limited. so I don't chose that route anymore.




    "Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message
    news:%23$E%...
    > I am passing an xml data file to a server control and need to sort the

    data. The data has three levels(tables).
    >
    > What is the recommended approach to do the sorting ?
    >
    > (using vb.net and asp.net 2.0)
    >
    >
    sloan, Feb 21, 2006
    #2
    1. Advertising

  3. Jon   Paal

    Jon Paal Guest

    are there any working examples of this somewhere ?
    I can't find my way through all the steps to get this working.

    I can get a dataset but it apparently isn't strongly typed, so the select isn't available





    "sloan" <> wrote in message news:...
    >
    > When I have to deal with alot of sorting.... I usually put everything in a
    > Strongly Typed DataSet.
    >
    > If you have have control over the XML being sent... you can send it as
    > xml... to mimick the DataSet schema.
    >
    > ..
    >
    > If not, you may have to do a Xml to Xml transformation .. to get the xml
    > formatted correctly.
    >
    > MyStronglyTypedDataSet ds = new MyStrongTypedDataSet();
    >
    > ds.//Code here to get your xml into the DataSet
    >
    > ds.Select("", "LastName, FirstName, Age Desc");
    >
    > the first parameter is the filter... which you set to empty string... the
    > second parameter is the sortby.
    >
    > Look up the DataSet.Select command.
    >
    > xsl sorting is pretty ... limited. so I don't chose that route anymore.
    >
    >
    >
    >
    > "Jon Paal" <Jon[ nospam ]Paal @ everywhere dot com> wrote in message
    > news:%23$E%...
    >> I am passing an xml data file to a server control and need to sort the

    > data. The data has three levels(tables).
    >>
    >> What is the recommended approach to do the sorting ?
    >>
    >> (using vb.net and asp.net 2.0)
    >>
    >>

    >
    >
    Jon Paal, Feb 21, 2006
    #3
  4. Jon   Paal

    carl Guest

    If you already have xml and you need to generate xml, xsl may be the
    easiest route. Here's a sample xml file and the transform to sort by
    order id, and by quantity.

    (Data.xml)

    <?xml version="1.0" encoding="utf-8" ?>
    <orders>
    <order id="789">
    <items>
    <item>
    <name>paint</name>
    <price>2</price>
    <quantity>30</quantity>
    </item>
    <item>
    <name>glue</name>
    <price>10</price>
    <quantity>2</quantity>
    </item>
    </items>
    </order>
    <order id="123">
    <items>
    <item>
    <name>sprockets</name>
    <price>1</price>
    <quantity>50</quantity>
    </item>
    <item>
    <name>cogs</name>
    <price>20</price>
    <quantity>5</quantity>
    </item>
    </items>
    </order>
    <order id="456">
    <items>
    <item>
    <name>nails</name>
    <price>2</price>
    <quantity>100</quantity>
    </item>
    </items>
    </order>
    </orders>

    (Sort.xsl)

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">

    <xsl:eek:utput method="xml" indent="yes"/>

    <xsl:template match="orders">
    <orders>
    <xsl:for-each select="order">
    <xsl:sort data-type="number" select="@id" />
    <order>
    <xsl:attribute name="id">
    <xsl:value-of select="@id"></xsl:value-of></xsl:attribute>
    <xsl:apply-templates select="items" />
    </order>
    </xsl:for-each>
    </orders>
    </xsl:template>

    <xsl:template match="items">
    <xsl:for-each select="item">
    <xsl:sort data-type="text" select="name" />
    <item>
    <name><xsl:value-of select="name"/></name>
    <price><xsl:value-of select="price" /></price>
    <quantity><xsl:value-of select="quantity" /></quantity>
    </item>
    </xsl:for-each>
    </xsl:template>
    </xsl:stylesheet>

    You can use an XSLCompiledTransform to do the sort, like this (sorry
    for the C#):

    static void Main(string[] args)
    {
    XmlTextReader xmlSource = new XmlTextReader("Data.xml");
    XPathDocument xpathDoc = new XPathDocument(xmlSource);

    XmlTextReader xslSource = new XmlTextReader("Sort.xsl");
    XslCompiledTransform xsltDoc = new XslCompiledTransform();
    xsltDoc.Load(xslSource);

    StringBuilder sb = new StringBuilder();
    TextWriter tw = new StringWriter(sb);

    xsltDoc.Transform(xpathDoc, null, tw);

    Console.WriteLine(sb.ToString());

    Console.WriteLine("Press any key to continue...");
    Console.ReadLine();
    }

    If you have other differences between your source and destination
    schema you can transform it in this step too.

    -Carl
    carl, Feb 21, 2006
    #4
  5. Jon   Paal

    Jon Paal Guest

    thanks, this solved my problem.
    Jon Paal, Feb 22, 2006
    #5
    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. Matthew Louden
    Replies:
    1
    Views:
    6,872
    Scott M.
    Oct 11, 2003
  2. Replies:
    2
    Views:
    1,400
    James Kanze
    Jul 6, 2010
  3. Jon   Paal

    sorting xml data in server control

    Jon Paal, Feb 21, 2006, in forum: ASP .Net Building Controls
    Replies:
    4
    Views:
    139
    Jon Paal
    Feb 22, 2006
  4. Jason
    Replies:
    0
    Views:
    368
    Jason
    Oct 4, 2006
  5. Tom Kirchner

    sorting by multiple criterias (sub-sorting)

    Tom Kirchner, Oct 11, 2003, in forum: Perl Misc
    Replies:
    3
    Views:
    461
    Michael Budash
    Oct 11, 2003
Loading...

Share This Page