Solved

Create XML String from DataView?

Posted on 2007-04-02
4
725 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…

808 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