在新的XmlRecordsetWriter中为Streams实现XmlTextWriter

时间:2008-11-19 05:11:59

标签: c# xml dataset recordset

有关背景信息,请参阅我的问题here

所以问题现在不是我不能向经典ASP发送DataSet,而是它无法对它做任何事情。所以我找到了一些代码来从DataSet创建记录集xml结构。

我从它的原始source稍微调整了一下。问题是我似乎无法提取基本流并使用它而不必写入文件。我错过了什么?

以下是我尝试测试课程的方法:

[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
}

}

2 个答案:

答案 0 :(得分:1)

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

您可以使用以下代码将DataTable转换为ADODB.RecordsetSimplest code to convert an ADO.NET DataTable to an ADODB.Recordset。 因此,您可以在下一个代码中使用ConvertToRecordset()方法。

现在只需要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);

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

答案 1 :(得分:0)

首先,行:

theStream = (MemoryStream)theWriter.BaseStream;

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

你想要的只是:

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字符串,您可以将其加载到XPathDocumentXmlDocument并随意播放。