Reading a Dataset from an XML file - want a fileless solution

B

Brian Parker

Here's a snippet of code I have:
==============================================
DataSet ds = new DataSet();

string strXMLFileName = Path.GetTempFileName();

StreamWriter sw = File.AppendText( strXMLFileName );
sw.WriteLine(@"<?xml version='1.0'?>");
sw.WriteLine(@"<Results>");

ServiceReply SearchResultData = CategoryWebService.EndRetrieveStateData(
iaCategoryHandle );
sw.WriteLine( SearchResultData.CategoryXML[ 0 ] );

sw.WriteLine(@"</Results>");
sw.Close();

ds.ReadXml( strXMLFileName );
==============================================

"SearchResultData.CategoryXML[ 0 ]" is some XML returned from a web
service. I want to take this XML, prepend it with the 2 lines and
append the last line and then convert it into a dataaset object. But, I
don't want to use a file if possible.

How can get I accomplish what is being done above without the use of the
file?

-BEP
 
Z

Zark3

What you might try is to deserialize your
SearchResultData.CategoryXML[0] back into an object and adding it (and
any successive search results if needed) to a collection object/an
array used to feed the dataset (be it one-at-a-time or after
re-serializing the collection to XML)

HTH,
Chris
 
G

Guest

DataSet ds = new DataSet();
StringBuilder sb = new StringBuilder();
sb.Append(@"<?xml version='1.0'?>");
sb.Append(@"<Results>");
ServiceReply SearchResultData = CategoryWebService.EndRetrieveStateData(
iaCategoryHandle );
sb.Append( SearchResultData.CategoryXML[ 0 ] );
sb.Append(@"</Results>");
byte[] b = System.Text.Encoding.UTF8.GetBytes(sb.ToString());
MemoryStream ms = new MemoryStream(b);
ds.ReadXml( ms);
 
S

sloan

I don't know if this helps or not.

C#




private DataSet GetDataSet1()
{
DataSet ds = new DataSet();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<?xml version=\"1.0\"?><items>");
sb.Append("<item>");
sb.Append("<key>abc</key>");
sb.Append("<value>Apple Berry Cat</value>");
sb.Append("<time>" + DateTime.Now.ToLongTimeString() + "</time>");
sb.Append("</item>");
sb.Append("<item>");
sb.Append("<key>def</key>");
sb.Append("<value>Dough Elephant Fence</value>");
sb.Append("<time>" + DateTime.Now.ToLongTimeString() + "</time>");
sb.Append("</item>");
sb.Append("<item>");
sb.Append("<key>hij</key>");
sb.Append("<value>House Igloo Jumprope</value>");
sb.Append("<time>" + DateTime.Now.ToLongTimeString() + "</time>");
sb.Append("</item>");
sb.Append("</items>");
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.StreamWriter writer = new System.IO.StreamWriter(ms);
writer.Write(sb.ToString());
writer.Flush();
ms.Position = 0;
ds.ReadXml(ms);
return ds;
}



VB.NET


Private Function GetDataSet1() As DataSet
Dim ds As New DataSet

Dim sb As New System.Text.StringBuilder
sb.Append("<?xml version=""1.0""?><items>")





sb.Append("<itemid>20002</itemid>")
sb.Append("<friendlyname1>Macintosh</friendlyname1>")
sb.Append("<friendlyname2>MA</friendlyname2>")
sb.Append(("<time>" + DateTime.Now.ToLongTimeString() + "</time>"))
sb.Append("<parentid>2001</parentid>")
sb.Append("</item>")

sb.Append("</items>")



Dim ms As New System.IO.MemoryStream
Dim writer As New System.IO.StreamWriter(ms)
writer.Write(sb.ToString())
writer.Flush()
ms.Position = 0
ds.ReadXml(ms)
Return ds
End Function 'GetDataSet1
 
B

Brian Parker

sloan said:
I don't know if this helps or not.
private DataSet GetDataSet1()
{
DataSet ds = new DataSet();
System.Text.StringBuilder sb = new System.Text.StringBuilder();
sb.Append("<?xml version=\"1.0\"?><items>"); ** STUFF SNIPPED **
sb.Append("</items>");
System.IO.MemoryStream ms = new System.IO.MemoryStream();
System.IO.StreamWriter writer = new System.IO.StreamWriter(ms);
writer.Write(sb.ToString());
writer.Flush();
ms.Position = 0;
ds.ReadXml(ms);
return ds;
}

I think that's exactly what I need.
Thanks,
-BEP
 

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