Question

I am using below piece of code to save the data to xml file in Windows Phone. First i am checking whether target xml file exists in the isolated storage or not; if it doesn't exists, i am creating the file and adding the required element data. If file exists, first checking whether element is already exists if so i am updating the attribute values, otherwise adding new element to the xml file.

The problem i am seeing is, if already element exists and trying to update the attributes (w/ below code) - i am seeing extra element added with new data and old data is still exists in the file. It is not updating instead appending.

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);
                    }
                }
            }
Was it helpful?

Solution

Set your open stream position to 0 or save XML document in the newly opened stream.

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);
}       
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top