Solved

Create XML String from DataView?

Posted on 2007-04-02
4
708 Views
Last Modified: 2013-11-07
Using VWD 2005 and SQL 2005

What started out to be a quick and dirty tool has proved to be quite a distraction for me.  I have a need to retrieve records from a stored procedure, serialize them into XML and run a transformation on them with an XSL.

My first thought was to create a DAL.  So, using the dataset wizard I made a data object with one method to retrieve records with a parameter.

Next I dropped an XML object onto my web form.  I set the TransformationSource property to my XSL stylesheet and my thought was to set the DocumentContent property to an XML string.

Where was the XML content to come from?  I knew that the dataset has the GetXML method.  However, the DAL tableadapters return a datatable.  I can make a dataview from that, but that doesn't really help either.

I know that I can just code this by hand in a few minutes and get the XML from a dataset, but like I said - this really got me distracted - and I was wondering if there was a way to serialize the contents of the datatable or dataview that is returned.
0
Comment
Question by:manicsquirrel
  • 2
4 Comments
 
LVL 5

Expert Comment

by:jef06
ID: 18841615
Try this :)

DataView data = new DataSet().Tables[0].DefaultView;
XmlSerializer serializer = new XmlSerializer(data.GetType());
                StringBuilder sb = new StringBuilder();
                TextWriter writer = new StringWriter(sb);

                serializer.Serialize(writer, data);
                StringReader reader = new StringReader(writer.ToString());

                DataSet dst = new DataSet();
                dst.ReadXml(reader);
                dv = new DataView(dst.Tables[0]);
0
 
LVL 28

Accepted Solution

by:
iboutchkine earned 500 total points
ID: 18842706
Serialize
\\\\
 Dim sw As New System.IO.StringWriter
 ds.WriteXml(sw)
 Dim mystring As String = sw.tostring
///
Deserialize
\\\
 Dim sr As New System.IO.StringReader(mystring)
 Dim ds2 As New DataSet
 ds2.ReadXml(sr)
///
0
 
LVL 6

Author Comment

by:manicsquirrel
ID: 18844222
I like using my brain a little, and while iboutchkine didn't write the routing for me, it was just enough to make a light bulb go off.  My resulting solution was (remember, I already have a dataview - dv)

        Dim sw As New System.IO.StringWriter
        dv.Table.WriteXml(sw)
        Dim str As String = sw.ToString

Now I have effectively retrieved serialized data from my dataview which is what I was wanting.  Thanks for both of your responses.
0
 
LVL 6

Author Comment

by:manicsquirrel
ID: 18844231
Did I type "routing"?  I meant "routine".
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes relatively difficult and non-obvious issues that are likely to arise when creating COM class in Visual Studio and deploying it by professional MSI-authoring tools. It is assumed that the reader is already familiar with the cla…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.

912 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now