Placing dataset into XPathDocument

  • Thread starter Thread starter Keith Chadwick
  • Start date Start date
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
 
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
 
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
 
Back
Top