Question

To use a XML serialized Dictionary, I use the SerializableDictionary class. For write and read a SerializableDictionary in a TXT file, I use these methods:

// Write
public static void WriteOnFile(string pathFile)
{
   XmlSerializer serializer = new XmlSerializer(typeof(SerializableDictionary<string, double>));
   using (FileStream fs = File.OpenWrite(pathFile))
   {
      using (StreamWriter writer = new StreamWriter(fs))
      {
         serializer.Serialize(fs, databaseMateriali);
      }
   }
}

// Read
public static SerializableDictionary<string, double> ReadFromFile(string pathFile)
{
   SerializableDictionary<string, double> elencoLetto = new SerializableDictionary<string, double>();
   using (FileStream fs = File.OpenRead(pathFile))
   {
      using (StreamReader reader = new StreamReader(fs))
      {
         fs.Position = 0;
         XmlSerializer serializer = new XmlSerializer(typeof(SerializableDictionary<string, double>));
         SerializableDictionary<string, double> clone = (SerializableDictionary<string, double>)serializer.Deserialize(fs);
                    elencoLetto = clone;
      }
   }
   return elencoLetto;
}

How can I delete an XML element from this TXT file, without erasing and rewriting the entire file?

Was it helpful?

Solution

There is no way to remove bytes from a file. You can mark your data so you can have a program logic to not use it. That way the file stays the same number of bytes but your logic can "delete" parts of it. But your class does not seem to support this, so you will have to just rewrite the entire file after removing the entry in memory.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top