Aggiornamento di un file XML esistente in Windows Phone
-
22-12-2019 - |
Domanda
Sto usando sotto il codice di codice per salvare i dati in file XML in Windows Phone.Per prima cosa sto verificando se il file XML di destinazione esiste nello storage isolato o meno; Se non esiste, sto creando il file e aggiungo i dati degli elementi richiesti.Se il file esiste, per prima cosa verificare se l'elemento è già esistente se è così aggiornando i valori degli attributi, altrimenti aggiungendo un nuovo elemento al file XML.
Il problema che sto vedendo è, se già Elemento esiste e il tentativo di aggiornare gli attributi (w / codice sotto) - Vedo un elemento extra aggiunto con nuovi dati e i vecchi dati sono ancora nel file.Non si aggiorna invece aggiungere.
using (IsolatedStorageFile storage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (storage.FileExists(fileName))
{
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(fileName, FileMode.Open, storage))
{
XDocument doc = XDocument.Load(isoStream);
bool isUpdated = false;
foreach (var item in (from item in doc.Descendants("Employee")
where item.Attribute("name").Value.Equals(empName)
select item).ToList())
{
// updating existing employee data
// element already exists, need to update the existing attributes
item.Attribute("name").SetValue(empName);
item.Attribute("id").SetValue(id);
item.Attribute("timestamp").SetValue(timestamp);
isUpdated = true;
}
if (!isUpdated)
{
// adding new employee data
doc.Element("Employee").Add(
new XAttribute("name", empName),
new XAttribute("id", id),
new XAttribute("timestamp", timestamp));
}
doc.Save(isoStream);
}
}
else
{
// creating XML file and adding employee data
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(fileName, FileMode.Create, storage))
{
XDocument doc = new XDocument(new XDeclaration("1.0", "utf8", "yes"),
new XElement("Employees",
new XElement("Employee",
new XAttribute("name", empName),
new XAttribute("id", id),
new XAttribute("timestamp", timestamp))));
doc.Save(isoStream, SaveOptions.None);
}
}
}
. Soluzione
Imposta la posizione di flusso aperto su 0 o salvare il documento XML nel flusso appena aperto.
XDocument doc = null;
using (IsolatedStorageFileStream isoStream = new IsolatedStorageFileStream(fileName, FileMode.Open, storage))
{
doc = XDocument.Load(isoStream);
bool isUpdated = false;
foreach (var item in (from item in doc.Descendants("Employee")
where item.Attribute("name").Value.Equals(empName)
select item).ToList())
{
// updating existing employee data
// element already exists, need to update the existing attributes
item.Attribute("name").SetValue(empName);
item.Attribute("id").SetValue(id);
item.Attribute("timestamp").SetValue(timestamp);
isUpdated = true;
}
if (!isUpdated)
{
// adding new employee data
doc.Element("Employee").Add(
new XAttribute("name", empName),
new XAttribute("id", id),
new XAttribute("timestamp", timestamp));
}
//First way
//isoStream.Position = 0;
//doc.Save(isoStream);
}
//Or second way
using (var stream = storage.OpenFile(fileName, FileMode.Open, FileAccess.Write))
{
doc.Save(stream);
}
.