Transforming a typed dataset

M

Matthew Baskey

Hello,

I need to reorder a Typed DataSet in memory using an Xsl Transform.

Most of the examples use stream which write to the hard disk or xml
file. I want to do this in memory and then write the resulting Typed
Dataset into a Session object which will be reused on multiple
postbacks
and then finally saved when editing is done. Get various problems
depending
on what objects I use, rather than go through each one I will post
what I
have and see if anyone can suggest where to go from here.

Xsl is simple, reordering (this is done becuase onitemupdate of
datagrid I delete element then add a new one. this is because there
are different types of columns and the use="optional" in the xsd file
didn't give me an extra method to remove by column
row.MultipleSelect.Delete() Anyhow this is peripheral as I am already
down the alley which I hope isn't blind.

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:blush:utput omit-xml-declaration="yes"/>
<xsl:template match="/*">
<Test>
<xsl:for-each select="/Test/Question">
<xsl:sort select="@id" order="ascending" data-type="number"/>
<xsl:copy-of select="."/>
</xsl:for-each>
</Test>
</xsl:template>
</xsl:stylesheet>

Method for reordering DataSet:

try
{
XmlDataDocument xmlDoc = new XmlDataDocument(ds);

XslTransform xslTran = new XslTransform();
xslTran.Load(Server.MapPath("QuestionSort.xslt"));

System.IO.MemoryStream s = new System.IO.MemoryStream();
System.Xml.XmlTextWriter w = new XmlTextWriter( s,
System.Text.Encoding.Default );

xslTran.Transform(xmlDoc, (XsltArgumentList)null,w,(XmlResolver)null);

s.Position = 0; //reset position of the in-memory stream.
StreamReader r = new StreamReader(s);

Test dsnew = new Test();
dsnew.ReadXml( r.ReadToEnd() );

//this method saves to session object
setDataSet(SessionTestName, dsnew);
}
catch (Exception ex)
{
ex = ex;
}

Thanks,
matt
 
M

MattB

After ploding through reams of demo code writing out to the
Response.OutputStream ....I pieced various bits of newsgroup snippets
to produce this which works.....reproduced here for frantic google
searchers.


try
{
XmlDataDocument xmlDoc = new XmlDataDocument(ds); //ds is input
dataset
XslTransform xslTran = new XslTransform();
xslTran.Load(Server.MapPath("QuestionSort.xslt"));

System.IO.MemoryStream memstr = new System.IO.MemoryStream();
System.Xml.XmlTextWriter writer = new XmlTextWriter( memstr,
System.Text.Encoding.Default );

xslTran.Transform(xmlDoc,
(XsltArgumentList)null,writer,(XmlResolver)null);

writer.Flush();

memstr.Position = 0;

Test dsnew = new Test(); //this is my typed dataset
dsnew.ReadXml( memstr );

setDataSet(SessionTestName, dsnew); //here I save the transformed
dataset to the session object
}
catch (Exception ex)
{
ex = ex;
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top