.net WriteXmlSchema ReadXmlSchema Stream Root Element Missing

So today i decided i needed to clone the schema of a dataset and then put it in an empty dataset. Once this was done i could selectively move rows to the new dataset.

The first thing i did was to read the data into an empty memorystream object.

Dim tempstream As New IO.MemoryStream
skeyobj.AssessmentDataset.WriteXmlSchema(tempstream)

Next i would put read that into the new dataset

newskey.AssessmentDataset.ReadXmlSchema(tempstream)

Now common since would make me think this would work and i would just be moving on, but………. This was not the case. I got an error “Root Element is missing”?

How? I just copied it from one dataset to the other. Well upon further inspection you realize the position of the pointer in the memorystream is still at the end of the file, so when you pass it to the readxmlschema function it starts the read from that point at the end and not the beginning.

Solution? Well that was simple. Just set the streams pointer position back to 0 and you are good to go.

tempstream.Position = 0

So in the end this is what it should look like together.

skeyobj.AssessmentDataset.WriteXmlSchema(tempstream)
tempstream.Position = 0
newskey.AssessmentDataset.ReadXmlSchema(tempstream)

I would have figured the read would move the position back itself, but we all know what happens when we make assumptions……EXCEPTIONS!!!!!!!!!!!!

 

This entry was posted in Uncategorized and tagged , , , , , . Bookmark the permalink.

6 Responses to .net WriteXmlSchema ReadXmlSchema Stream Root Element Missing

Leave a Reply

Your email address will not be published.