0

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();
        }
    }
niks
  • 579
  • 1
  • 3
  • 14
  • Why can't you just read it into memory and then use it as a value in your statement? – tadman Apr 07 '17 at 09:50
  • 1
    You can [Convert the XDocument to a Stream](http://stackoverflow.com/questions/750198/convert-xdocument-to-stream) which should do it. – stuartd Apr 07 '17 at 09:50
  • @tadman,@stuartd. Thank you for answering. I have edited my sample code. Can you please take a look if it is the correct way? I am beginner at this stuff. – niks Apr 07 '17 at 10:49

0 Answers0