问题
For background see my question here.
So the problem now isn't that I can't send a DataSet
to classic ASP but that it can't do anything with it. So I found some code to create a recordset xml structure from a DataSet
.
I have tweaked it a little from it's original source. The problem is that I can't seem to extract the base stream and use it instead of having to write to a file. What am I missing?
Here is how I am trying to test the class:
[Test]
public void TestWriteToStream()
{
MemoryStream theStream = new MemoryStream();
XmlRecordsetWriter theWriter = new XmlRecordsetWriter(theStream);
theWriter.WriteRecordset( SomeFunctionThatReturnsADataSet() );
theStream = (MemoryStream)theWriter.BaseStream;
string xmlizedString = UTF8ByteArrayToString(theStream.ToArray());
xmlizedString = xmlizedString.Substring(1);
//Assert.AreEqual(m_XMLNotNull, xmlizedString);
Console.WriteLine(xmlizedString);
}
Here is my class:
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Text;
using System.Xml;
namespace Core{
public class XmlRecordsetWriter : XmlTextWriter
{
#region Constructors
// Constructor(s)
public XmlRecordsetWriter(string filename) : base(filename, null) { SetupWriter(); }
public XmlRecordsetWriter(Stream s) : base(s, null) { SetupWriter(); }
public XmlRecordsetWriter(TextWriter tw) : base(tw) { SetupWriter(); }
protected void SetupWriter()
{
base.Formatting = Formatting.Indented;
base.Indentation = 3;
}
#endregion
#region Methods
// WriteRecordset
public void WriteRecordset(DataSet ds) { WriteRecordset(ds.Tables[0]); }
public void WriteRecordset(DataSet ds, string tableName) { WriteRecordset(ds.Tables[tableName]); }
public void WriteRecordset(DataView dv) { WriteRecordset(dv.Table); }
public void WriteRecordset(DataTable dt)
{
WriteStartDocument();
WriteSchema(dt);
WriteContent(dt);
WriteEndDocument();
}
// WriteStartDocument
public void WriteStartDocument()
{
base.WriteStartDocument();
base.WriteComment("Created by XmlRecordsetWriter");
base.WriteStartElement("xml");
base.WriteAttributeString("xmlns", "s", null, "uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882");
base.WriteAttributeString("xmlns", "dt", null, "uuid:C2F41010-65B3-11d1-A29F-00AA00C14882");
base.WriteAttributeString("xmlns", "rs", null, "urn:schemas-microsoft-com:rowset");
base.WriteAttributeString("xmlns", "z", null, "#RowsetSchema");
}
// WriteSchema
public void WriteSchema(DataSet ds) { WriteSchema(ds.Tables[0]); }
public void WriteSchema(DataSet ds, string tableName) { WriteSchema(ds.Tables[tableName]); }
public void WriteSchema(DataView dv){ WriteSchema(dv.Table); }
public void WriteSchema(DataTable dt)
{
// Open the schema tag (XDR)
base.WriteStartElement("s", "Schema", null);
base.WriteAttributeString("id", "RowsetSchema");
base.WriteStartElement("s", "ElementType", null);
base.WriteAttributeString("name", "row");
base.WriteAttributeString("content", "eltOnly");
// Write the column info
int index=0;
foreach(DataColumn dc in dt.Columns)
{
index ++;
base.WriteStartElement("s", "AttributeType", null);
base.WriteAttributeString("name", dc.ColumnName);
base.WriteAttributeString("rs", "number", null, index.ToString());
base.WriteEndElement();
}
// Close the schema tag
base.WriteStartElement("s", "extends", null);
base.WriteAttributeString("type", "rs:rowbase");
base.WriteEndElement();
base.WriteEndElement();
base.WriteEndElement();
}
// WriteContent
public void WriteContent(DataSet ds) { WriteContent(ds.Tables[0]); }
public void WriteContent(DataSet ds, string tableName) { WriteContent(ds.Tables[tableName]); }
public void WriteContent(DataView dv) { WriteContent(dv.Table); }
public void WriteContent(DataTable dt)
{
// Write data
base.WriteStartElement("rs", "data", null);
foreach(DataRow row in dt.Rows)
{
base.WriteStartElement("z", "row", null);
foreach(DataColumn dc in dt.Columns)
base.WriteAttributeString(dc.ColumnName, row[dc.ColumnName].ToString());
base.WriteEndElement();
}
base.WriteEndElement();
}
// WriteEndDocument
public void WriteEndDocument()
{
base.WriteEndDocument();
base.Flush();
base.Close();
}
#endregion
}
}
回答1:
I think you want to work with Data-based objects and XML based data.
If it's true, I suggest using ADODB
Classes (It is in COM References: Microsoft ActiveX Data Objects 6.0 Library -Or in other versions like 2.8-).
You can convert your DataTable
to a ADODB.Recordset
by this code: Simplest code to convert an ADO.NET DataTable to an ADODB.Recordset.
So you have a ConvertToRecordset()
method to use in next code.
Now you need only save()
method to have your XML file:
using ADODB;
using System;
using System.Data;
using System.IO;
namespace ConsoleApplicationTests
{
class Program
{
static void Main(string[] args)
{
Recordset rs = new Recordset();
DataTable dt = sampleDataTable(); //-i. -> SomeFunctionThatReturnsADataTable()
//-i. Convert DataTable to Recordset
rs = ConvertToRecordset(dt);
//-i. Sample Output File
String filename = @"C:\yourXMLfile.xml";
FileStream fstream = new FileStream(filename, FileMode.Create);
rs.Save(fstream, PersistFormatEnum.adPersistXML);
}
}
}
The power of ADODB.Recordset
is here that you can open that saved XML file very easy:
rs.Open(fstream);
I hope it works!, Actually I wrote this answer to complete it later and if I'm in a right direction.
回答2:
First, the line:
theStream = (MemoryStream)theWriter.BaseStream;
is redundant as the MemoryStream
should already be the base stream for the writer.
What it sounds like you want is just:
theWriter.Close();
theStream.Position = 0; // So you can start reading from the begining
string xml = null;
using (StringReader read = new StringReader(theStream))
{
xml = read.ReadToEnd();
}
Then xml will be your xml string that you can load into an XPathDocument
or XmlDocument
and play with however you want.
来源:https://stackoverflow.com/questions/301045/implementing-xmltextwriter-in-new-xmlrecordsetwriter-for-streams