I am converting 3D objects to one xml file, write it to hard drive, then read it from hard drive and upload to Mysql database as a blob. Then I delete created xml.file from my hard drive. Is it somehow possible to do this (create xml file/upload to database) so that I don't have to write it to drive at first, then read it, then delete it? If I could somehow create xml file and pass it directly without the need to save it on drive. Any ideas?
Here I write xml file to drive (EDITED):
public Stream WriteXml(List<object> gridEntities, string fileName)
{
XDocument doc = new XDocument();
XElement root = new XElement("ViewportLayout");
XElement xEntities = new XElement("Entities");
xEntities.Add(...);
root.Add(xEntities);
doc.Add(root);
//var path = string.Format("C:\\Users\\NP\\Desktop\\Saves\\{0}", fileName);
//doc.Save(path);
Stream stream = new MemoryStream(); // Create a stream
doc.Save(stream); // Save XDocument into the stream
stream.Position = 0; // Rewind the stream ready to read from it elsewhere
return stream;
}
Here I upload it to data base:
public static void SaveGridXmlFileToDataBase(Stream stream, string projectName, string filePath, string gridName, string gridGuid)
{
if (OpenConnection() == true)
{
byte[] file;
using (stream)
{
using (var reader = new BinaryReader(stream))
{
file = reader.ReadBytes((int)stream.Length);
}
}
string project = string.Concat(projectName, "_grids");
string query = String.Format("INSERT INTO {0} SET name=@name, guid=@guid, xml=@File;", project);
using (var sqlWrite = new MySqlCommand(query, connection))
{
sqlWrite.Parameters.Add("@name", MySqlDbType.VarChar).Value = gridName;
sqlWrite.Parameters.Add("@guid", MySqlDbType.VarChar).Value = gridGuid;
sqlWrite.Parameters.Add("@File", MySqlDbType.LongBlob).Value = file;
sqlWrite.ExecuteNonQuery();
}
CloseConnection();
}
}