Getting Filtered XML from dataset

G

Guest

Hi All

I have a dataset which has a table
This data table contains 100 rows

My requirement is I need to first filter these rows bases on some condition and then generate the XML of the dataset

How can I do that

I could filter the records using RowFilter method . But the GetXml method of the dataset always returns the original data (100 rows

I could filter the data in the table using the follg code

table = dataset.Tables[0]
table.DefaultView.RowStateFilter = DataViewRowState.OriginalRows
table.DefaultView.RowFilter = "ColumnA= 'AAA' "

After this how can I generate the new XML (with records matching the above criteria)

Any Ideas

Thanks & Regards
Ra
 
S

Sahil Malik

Here's what you've gotta do.

DataView dv = table.DefaultView ;
DataTable dtTemp = dv.Table.Clone();
dtTemp.TableName = "Row"; // this is arbitrary!

// Populate the table with rows in the view
foreach(DataRowView drv in dv)
dtTemp.ImportRow(drv.Row);

// giving a custom name to the DataSet can help to
// come up with a clearer layout but is not mandatory
DataSet dsTemp = new DataSet(dv.Table.TableName);

// Add the new table to a temporary DataSet
dsTemp.Tables.Add(dtTemp);
dsTemp.WriteXml(.....blah blah).

The only real bridge between XML and System.Data is centered around the
DataSet island <--- gonna change in ADO.NET 2.0

In ADO.NET 2.0, DataTables will be serializable, so you could use
StreamWriter to serialize them as XML.

Alternatively, if you need to do this over and over in your project, I would
create a new class, inheriting from DataTable, and Implementing
IXMLSerializable ..and instead of using "DataTable", use this class instead
nad Serialize it to XML wherever you wish.

- Sahil Malik
Independent Consultant
You can reach me thru my blog at -
http://www.dotnetjunkies.com/weblog/sahilmalik/



Ram said:
Hi All,

I have a dataset which has a table.
This data table contains 100 rows.

My requirement is I need to first filter these rows bases on some
condition and then generate the XML of the dataset.
How can I do that?

I could filter the records using RowFilter method . But the GetXml method
of the dataset always returns the original data (100 rows)
I could filter the data in the table using the follg code.

table = dataset.Tables[0];
table.DefaultView.RowStateFilter = DataViewRowState.OriginalRows;
table.DefaultView.RowFilter = "ColumnA= 'AAA' ";

After this how can I generate the new XML (with records matching the above criteria)?

Any Ideas?

Thanks & Regards,
Ram
 
V

Val Mazur

Hi Ram,

You could loop or you could use a stylesheet. Assuming that your dataset has
a MYDS name and table inside of it has a MYTABLE name. If you would like to
filter on a column ColumnA, then your stylesheet would look like (save it as
Transform.xsl file)


<?xml version="1.0" ?>

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">

<xsl:template match="/">
<xsl:apply-templates />
</xsl:template>

<xsl:template match="/MYDS">

<xsl:element name="MYDS">

<xsl:copy-of select="MYTABLE[ColumnA='AAA']" />

</xsl:element>

</xsl:template>

</xsl:stylesheet>

Your VB code would look like


Dim loSourceXML As XPath.XPathDocument
Dim loTransform As Xsl.XslTransform
Dim loDataSet As DataSet
Dim loNewDataStream As MemoryStream
Dim loFilteredDataSet As DataSet

loSourceXML = New XPath.XPathDocument(New
StringReader(loDataSet.GetXml()))

loTransform = New Xsl.XslTransform()
loTransform.Load("C:\Transform.xsl")

loNewDataStream = New MemoryStream()

loTransform.Transform(loSourceXML, Nothing, loNewDataStream)
loFilteredDataSet = New DataSet()
loNewDataStream.Position = 0

loFilteredDataSet.ReadXml(loNewDataStream, XmlReadMode.Auto)
 

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