views:

9

answers:

0

I'm loading a datatable from the cache and I need to read it's schema, and then load that schema into another datatable. Here's the code I'm using:

    using (var ms = new MemoryStream())
    {
        using (var xmlWriter = XmlWriter.Create(ms))
        {
            tableWithSchema.WriteXmlSchema(xmlWriter);
            xmlWriter.Flush(); //tried this to fix it
            xmlWriter.Close(); //tried this to fix it
        }

        tableToLoadSchema.ReadXmlSchema(XmlReader.Create(ms));
    }

For some reason when reading the schema back in a I get an XmlException saying the Root node is missing. However, if I first write it to a file then read in that file it works just fine:

    using (var ms = new MemoryStream())
    {
        using (var xmlWriter = XmlWriter.Create("c:\\test.xml"))
        {
            tableWithSchema.WriteXmlSchema(xmlWriter);
            xmlWriter.Flush();
            xmlWriter.Close();
        }

        tableToLoadSchema.ReadXmlSchema(XmlReader.Create(File.OpenText("c:\\test.xml")));
    }

Any ideas of why using the MemoryStream isn't working?