Creating a DataSet with Data and Schema based on data in DataRow

A

Andy Sjostrom

Hi!
I have a DataSet (dsTracking) which contains a row is updated with DataSet
XML and inline Schema.
So, in a DataRow I have XML data and schema that can be used to create a
DataSet.
The XML data and schema was created using DataSet.WriteXml with the
appropriate XmlWriteMode:


ds.WriteXml(xtw, XmlWriteMode.WriteSchema);

How can I create a DataSet based on data in a DataRow?

Using DataSet.ReadXml () I can specify XmlReadMode.ReadSchema as the second
argument. But what about the first?


I have tried this:

DataSet ds = new DataSet();
MemoryStream ms = new MemoryStream();

StreamWriter sw = new StreamWriter(ms);

sw.Write(dsTracking.Tables["TrackingDataTable"].Rows[0]["DataSetXML"]);

ms.Seek(0, SeekOrigin.Begin);

StreamReader sr = new StreamReader(ms);

ds.ReadXml(sr.ReadToEnd(), XmlReadMode.ReadSchema);


The above code throws an exception at the ds.ReadXml... The exception is:
"Additional information: Invalid URI: The URI scheme is not valid."

Thanks,

Andy Sjostrom
MS MVP - Mobile Devices
 
C

Cor

Hi Andy,

I this sample something your are looking for?

\\\
DataSet ds = new DataSet();
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("test");
dt.Columns.Add(dc);
ds.Tables.Add(dt);
MemoryStream ms = new MemoryStream();
ds.WriteXml(ms,XmlWriteMode.WriteSchema);
ms.Position=0;
DataSet ds2 = new DataSet();
ds2.ReadXml(ms,XmlReadMode.ReadSchema);
ds2.WriteXml(@"C:\test1.xml",XmlWriteMode.WriteSchema);
///

I hope this helps?

Cor
 
C

Cor

Hi Andy,

I did make that dataset as a sample to do your problem, but I have another
sample, I still have.

The only problem for you is that it goes for you in the wrong direction, but
I think it cannot be that hard to change that in the right direction :)

It is also VB.net (But the changes are not that difficult to do in my
opinion to C#)

If you do nto succeed tell than I will look to it tomorrow.

Cor

Dim ds As New DataSet
Dim dt As New DataTable("parameters")
For c As Integer = 1 To 10
Dim dc As New DataColumn("elem" & c.tostring)
dt.Columns.Add(dc)
Next
For r As Integer = 1 To 10
Dim dr As DataRow = dt.NewRow
For c As Integer = 1 To 10
dr("elem" & c.tostring) = _
r.ToString & c.tostring ' or just dr(c) but to show you
Next
dt.Rows.Add(dr) ' can also before but I find this looking nicer
Next
ds.Tables.Add(dt)
-------------------------end of building of the testset

Dim ser As XmlSerializer = New XmlSerializer(GetType(DataSet))
Dim ms As New IO.MemoryStream
Dim sw As IO.TextWriter = New IO.StreamWriter(ms)
ser.Serialize(sw, ds)
Dim b As Long = ms.Length
ms.Position = 0
Dim sr As IO.TextReader = New IO.StreamReader(ms)
Dim xmlstring As String = sr.ReadToEnd
sw.Close()
sr.Close()
ms.Close()
 
A

Andy Sjostrom

Thanks, Cor!
I have code that goes in "the wrong direction". VB.NET is not an issue but I
wonder if the System.Text.Encoding might be...

Best regards,

-Andy.
 
A

Andy Sjostrom

Currently investigating:

ds.ReadXml( new
StringReader(dsTracking.Tables["TrackingDataTable"].Rows[0]["DataSetXML"].To
String()), XmlReadMode.ReadSchema );



which looks promising. Will report back if this solves my problem!



Andy Sjostrom

MS MVP - Mobile Devices
 
A

Andy Sjostrom

It works!
Full source code to appear on a web site near you soon!

Andy Sjostrom
MS MVP - Mobile Devices
 

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