更新Windows Phone中的现有xml文件

时间:2014-02-27 00:07:52

标签: c# xml windows-phone-8 isolatedstoragefile

我使用下面的代码将数据保存到Windows Phone中的xml文件。首先,我要检查目标xml文件是否存在于隔离存储中; 如果它不存在,我正在创建文件并添加所需的元素数据。如果文件存在,首先检查元素是否已存在,如果是,我更新属性值,否则将新元素添加到xml文件。

我看到的问题是,如果已经存在元素并且尝试更新属性(w /以下代码) - 我看到额外的元素添加了新数据,旧数据仍然存在于文件中。它没有更新而是附加。

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);
                    }
                }
            }

1 个答案:

答案 0 :(得分:1)

将您的开放流位置设置为0或将XML文档保存在新打开的流中。

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);
}