0

有关背景,请参阅我的问题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
}

}

4

2 回答 2

1

我认为您想使用基于数据的对象和基于 XML 的数据。如果是这样,我建议使用ADODB类(它在 COM 参考中:Microsoft ActiveX 数据对象 6.0 库 - 或其他版本,如 2.8-)。

您可以通过以下代码将您DataTable的转换为:将 ADO.NET DataTable 转换为 ADODB.Recordset 的最简单代码。所以你有一个方法可以在下一个代码中使用。ADODB.RecordsetConvertToRecordset()

现在您只需要save()获取 XML 文件的方法:

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);
        }
    }
}

的强大之处ADODB.Recordset在于,您可以非常轻松地打开保存的 XML 文件:

rs.Open(fstream);

我希望它有效!,实际上我写了这个答案是为了稍后完成它,如果我的方向是正确的。

于 2015-03-04T16:23:13.030 回答
0

首先,行:

theStream = (MemoryStream)theWriter.BaseStream;

是多余的,因为MemoryStream应该已经是 writer 的基本流。

听起来你想要的只是:

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();
}

然后 xml 将成为您的 xml 字符串,您可以将其加载到XPathDocumentorXmlDocument中并随心所欲地使用。

于 2008-12-18T18:38:50.380 回答