質問

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?

役に立ちましたか?

解決

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.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top