Note that there are some explanatory texts on larger screens.

plurals
  1. POImplementing XmlTextWriter in new XmlRecordsetWriter for Streams
    primarykey
    data
    text
    <p>For background see my question <a href="https://stackoverflow.com/questions/291761/converting-a-fairly-simple-c-class-library-into-a-com-object">here</a>.</p> <p>So the problem now isn't that I can't send a <code>DataSet</code> 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 <code>DataSet</code>.</p> <p>I have tweaked it a little from it's original <a href="http://www.microsoft.com/mspress/books/companion/6235.aspx#Companion%20Content" rel="nofollow noreferrer">source</a>. 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?</p> <p>Here is how I am trying to test the class:</p> <pre><code>[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); } </code></pre> <p>Here is my class:</p> <pre><code>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 } </code></pre> <p>}</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload