The follwing procedure to export a dataset to excel is working fine in a
web app except for a warning that XMLDataDocument class will be removed in a future release.
The subroutine stores the dataset ds into an xml file xdDoc and then uses a predefined xslt (XResult.xslt) and its transform method to format the xml file into an Excel .xls output file.
Can anybody help editing the procedure that would use XDocument and xslt style sheet instead of XMLDataDocument and xslt?
I think I am lazy enough not to explore the XDocument class and seeking help for such a trivial matter.
But nothing like getting a little bit of help from you all and giving a point or two in return
Please feel free to edit the code below. Links are welcome but appreciate if a functional vb code using XDocument is given too.
Thanks in advance.
Private Sub ExportToExcel(ByVal context As HttpContext, ByVal ds As DataSet, flname As String)
Dim xtw As XmlTextWriter
Dim xdDoc As XmlDataDocument = New XmlDataDocument(ds)
Dim xslTran As XslCompiledTransform = New XslCompiledTransform()
xslTran.Load(context.Server.MapPath("XResult.xslt"))
Dim flNameXLS As String
flNameXLS = flname + ".xls"
xtw = New XmlTextWriter(context.Server.MapPath(flNameXLS), System.Text.Encoding.UTF8)
xtw.Formatting = Formatting.Indented
xtw.Indentation = 3
xslTran.Transform(xdDoc, Nothing, xtw)
xtw.Close()
context.Response.Redirect(flNameXLS)
context.Response.End()
End Sub
Private Sub ExportToExcel(ByVal context As HttpContext, ByVal ds As DataSet, flname As String)
Dim xtw As XmlTextWriter
'''''Dim xdDoc As XmlDataDocument = New XmlDataDocument(ds)
Dim doc As New XDocument()
Using xw As XmlWriter = doc.CreateWriter()
ds.WriteXml(xw)
xw.Close()
End Using
Dim xslTran As XslCompiledTransform = New XslCompiledTransform()
xslTran.Load(context.Server.MapPath("XResult.xslt"))
Dim flNameXLS As String
flNameXLS = flname + ".xls"
xtw = New XmlTextWriter(context.Server.MapPath(flNameXLS), System.Text.Encoding.UTF8)
xtw.Formatting = Formatting.Indented
xtw.Indentation = 3
xslTran.Transform(doc, Nothing, xtw)
xtw.Close()
context.Response.Redirect(flNameXLS)
context.Response.End()
End Sub
However, it is throwing an error becacuase the trasform method won't take a XDocument for a
XmlDataDocument...!!!
In the transform method, we should use CreateReader method of the XDocument.
Private Sub ExportToExcel(ByVal context As HttpContext, ByVal ds As DataSet, flname As String)
Dim xtw As XmlTextWriter
'''''Dim xdDoc As XmlDataDocument = New XmlDataDocument(ds)
Dim doc As New XDocument()
Using xw As XmlWriter = doc.CreateWriter()
ds.WriteXml(xw)
xw.Close()
End Using
Dim xslTran As XslCompiledTransform = New XslCompiledTransform()
xslTran.Load(context.Server.MapPath("XResult.xslt"))
Dim flNameXLS As String
flNameXLS = flname + ".xls"
xtw = New XmlTextWriter(context.Server.MapPath(flNameXLS), System.Text.Encoding.UTF8)
xtw.Formatting = Formatting.Indented
xtw.Indentation = 3
xslTran.Transform(doc.CreateReader(), Nothing, xtw)
xtw.Close()
context.Response.Redirect(flNameXLS)
context.Response.End()
End Sub
Member
212 Points
165 Posts
XmlDataDocument to XDocument
Jan 22, 2014 04:49 AM|aspnetdev2000|LINK
Hi,
The follwing procedure to export a dataset to excel is working fine in a web app except for a warning that XMLDataDocument class will be removed in a future release.
The subroutine stores the dataset ds into an xml file xdDoc and then uses a predefined xslt (XResult.xslt) and its transform method to format the xml file into an Excel .xls output file.
Can anybody help editing the procedure that would use XDocument and xslt style sheet instead of XMLDataDocument and xslt?
I think I am lazy enough not to explore the XDocument class and seeking help for such a trivial matter.
But nothing like getting a little bit of help from you all and giving a point or two in return
Please feel free to edit the code below. Links are welcome but appreciate if a functional vb code using XDocument is given too.
Thanks in advance.
Regards,
xdocument xmldatadocument dataset excel
Participant
914 Points
177 Posts
Re: XmlDataDocument to XDocument
Jan 22, 2014 05:37 AM|Slewey|LINK
You can replace the XmlDataDocument with XmlDocument very easily without changing the rest of your code;
Or if you specifically need a XDocument;
xdocument xmldatadocument dataset excel
Member
212 Points
165 Posts
Re: XmlDataDocument to XDocument
Jan 22, 2014 10:14 AM|aspnetdev2000|LINK
Hi Slewey,
I guess you are asking me do this:
However, it is throwing an error becacuase the trasform method won't take a XDocument for a XmlDataDocument ...!!!
xdocument xmldatadocument dataset excel
Member
212 Points
165 Posts
Re: XmlDataDocument to XDocument
Jan 22, 2014 08:38 PM|aspnetdev2000|LINK
Ok, I figured that out.
In the transform method, we should use CreateReader method of the XDocument.
xdocument xmldatadocument dataset excel