Placing dataset into XPathDocument

K

Keith Chadwick

I have a merged dataset that contains xml read from SQL Server. I need to
place the data into an XPathDocument.

I can do the following:

mydataset.writeXML("mydata.xml")
dim xpdoc as new XPathDocument("mydata.xml")

Problem is it seem rather redundent to write data currently in memory to
disk in order to be read on the next line. According to the documentation
the writeXML method supports writing to System.IO.Stream and the
XPathDocument supports load from System.IO.Stream but I can not seem to get
this to work.

Any suggestions?

Cheers
Keith
 
G

Guest

Hello Keith,

I have a suggestion for you which worked really well for
me.

1. Create a string of your DataSet by using GetXml()
string something = mydataset.GetXml();
2. Create a new XmlDocument object and instantiate it
with DataSet string "something"
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.LoadXml(something);
3. Read the XmlDocument into the XPathDocument
XPathDocument xpath = new XPathDocument(new
XmlNodeReader(xmlDoc.DocumentElement());

Hope this helps!
Vivek
 
O

Oleg Tkachenko

S

Stephen Cook

Keith,

Using streams can get a bit interesting
I have a habit of tying myself in knots by creating one level too many of
writers and readers before I factor out all the rubbish again
I've "standardised" on the following overloads to methods that return
streams (callers for my earlier Transform dataset example)
Note should probably give thought to using exceptions to flag failure
//Accept a stream to handle web response
//Can be called from ASP.Net using HttpContext.Current.Response.OutputStream
//To write to the Console pass in Console.OpenStandardOutput()
public Boolean TransformDataset(System.IO.Stream strmOut)
{
Boolean flgSuccess;
strmOutput = new StreamWriter(strmOut,System.Text.Encoding.UTF8,640);
flgSuccess = prvTransformDataset();
strmOutput.Close();
return(flgSuccess);
}

//String argument defines file to create
public Boolean TransformDataset(string strOut)
{
Boolean flgSuccess;
strmOutput = new StreamWriter(strOut);
flgSuccess = prvTransformDataset();
strmOutput.Close();
return(flgSuccess);
}

//Overload that returns a string
//Uses an in memory stream as a buffer
//Not sure that this is the best way to achieve this
public string TransformDataset()
{
strmLocal = new MemoryStream();
strmLocal.Seek(0,SeekOrigin.Begin);
strmOutput = new StreamWriter(strmLocal,System.Text.Encoding.UTF8,640);
try
{
if(prvTransformDataset())
{
strmLocal.Position = 0;
StreamReader strmRdOut = new
StreamReader(strmLocal,System.Text.Encoding.UTF8);
return(strmRdOut.ReadToEnd().ToString());
}
else
{
return("Failed!");
}
}
finally
{
strmOutput.Close();
}
}

Stephen
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top