Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   ASP .Net Building Controls (http://www.velocityreviews.com/forums/f59-asp-net-building-controls.html)
-   -   sorting xml data in server control (http://www.velocityreviews.com/forums/t758202-sorting-xml-data-in-server-control.html)

Jon Paal 02-21-2006 07:54 AM

sorting xml data in server control
 
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 02-21-2006 04:24 PM

Re: sorting xml data in server control
 

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%23CwrNGHA.1192@TK2MSFTNGP11.phx.gbl...
> 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 02-21-2006 07:20 PM

Re: sorting xml data in server control
 
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" <sloan@ipass.net> wrote in message news:uxxO4NwNGHA.1676@TK2MSFTNGP09.phx.gbl...
>
> 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%23CwrNGHA.1192@TK2MSFTNGP11.phx.gbl...
>> 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)
>>
>>

>
>




carl 02-21-2006 11:06 PM

Re: sorting xml data in server control
 
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:output 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


Jon Paal 02-22-2006 07:19 PM

Re: sorting xml data in server control
 
thanks, this solved my problem.




All times are GMT. The time now is 07:46 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.