I have an ADO.NET DataSet object that I want to write to a document library for future reference (kind of a caching mechanism as this data doesn't change often but is read frequently).

I know that I can write this DataSet to a Stream, filename (including path), TextWriter or XmlWriter.

To write a document to my document library I need to use SPFileCollection.Add, probably providing a path, Stream object and a boolean indicating that I want to overwrite the existing file.

The problem is I can't figure out how to make this work. I've been trying to write the DataSet to a Stream, then passing this Stream into the SPFileCollection.Add method but it's not making much sense.

I'm wondering whether I'm approaching this wrongly. I hope someone can help me here.

  • You're right - "not making much sense" isn't very helpful. I'm not getting any errors at all. I can't figure out how to structure the code. Data is to be read later on by at least one web part, most likely a DataView. This library contains resources that are used by the site but users do not directly access the files in this doc library. My trouble is in how to provide this stream as a parameter to the DataSet.WriteXml method then as a parameter to the SPFileCollection.Add method. – Mike H Aug 11 '10 at 0:05

Using an ADO.NET DataSet while perhaps a good idea, didn't really work well in practice, though I'm quite willing to accept I wasn't handling my streams very well.

The problem arose when I called DataSet.WriteXml(Stream), then tried to use this Stream as the input for SPFileCollection.Add(string, Stream). I'm not sure whether I can write to a stream in this way, then consume it without doing something else in between.

I rejigged the code to give me an XmlDocument instead of the DataSet. Passing the Xmldocument.OuterXml into a byte array has worked better (see code below)

    // This section just sets up a sample System.Xml.XmlDocument
// In practice, this would be passed in from outside as a parameter
XmlDocument xdoc = new XmlDocument();

XmlElement xeData = xdoc.CreateElement("data");

XmlElement xeSample = xdoc.CreateElement("sample");
xeSample.InnerText = "Sample 1";

// The SPFileCollection.Add method takes either a byte array or a Stream
byte[] xmlbytes = new UnicodeEncoding().GetBytes(xdoc.OuterXml);

using (SPSite site = new SPSite("http://thesite"))
    using (SPWeb web = site.RootWeb)
        SPDocumentLibrary doclib = (SPDocumentLibrary)web.Lists["Test docs"];

        SPFolder folder = doclib.RootFolder;

        string desturl = web.Url + "/" + folder.Url + "/justadded.xml";
        SPFile addedfile = web.Files.Add(desturl, xmlbytes, true);
        SPListItem item = addedfile.Item;
        item["Title"] = "Just added this item";

EDIT: It's trivial to convert a DataSet to an XmlDocument using something like below:

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.