Solved

Create XML String from DataView?

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

760 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

26 Experts available now in Live!

Get 1:1 Help Now