有关背景,请参阅我的问题here。
所以现在的问题不是我不能发送DataSet
到经典的 ASP,而是它不能用它做任何事情。所以我找到了一些代码来创建一个记录集 xml 结构DataSet
。
我已经从它的原始来源对其进行了一些调整。问题是我似乎无法提取基本流并使用它,而不必写入文件。我错过了什么?
这是我尝试测试课程的方式:
[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);
}
这是我的课:
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
}
}