Load and Save TestPlan from a string xml formatted

Hi
I’m again here to ask I can solve the possibility of loading and saving a testplan not form / to a file but from an XML formatted string.
The purpose is to store test plans in a database available for multiple test systems. I could save the string in a temporary file and then apply the Load and Save functions of the TestPlan class, but I would like to know if there is a direct way without using a file.
Thanks for your help
Ralph

1 Like

There is an overload of TestPlan.Load and TestPlan.Save which takes a Stream. You can use those for any situation where you don’t have an actual file.

1 Like

The best candidate to do this is

TestPlan.Load(Stream stream,  string path)

but I code this with no success

// create a memory stream froma string
// xml is a string xml-formatted 
byte[] byteArray = Encoding.ASCII.GetBytes(xml);
MemoryStream stream = new MemoryStream(byteArray);

// Load test plan from string (path is null)
var testPlan = TestPlan.Load(stream, null);

// To check if all is OK save in a new memory stream
MemoryStream stream2 = new MemoryStream();
testPlan.Save(stream2);

// convert stream to string
StreamReader reader = new StreamReader(stream2);
string text = reader.ReadToEnd();
Console.WriteLine(text);

Nothing in text
Thanks

Ralph

1 Like

Hmm, I think you should rewind stream2 before reading from it with the stream reader.

//...
MemoryStream stream2 = new MemoryStream();
testPlan.Save(stream2);

// set stream2 position to the beginning for the 
stream2.Seek(0, SeekOrigin.Set); 

// convert stream to string
StreamReader reader = new StreamReader(stream2);
// ...
2 Likes

GREET!!! It’s works
Many Thanks!

2 Likes