0

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

我看到的问题是,如果已经存在元素并尝试更新属性(使用下面的代码) - 我看到添加了新数据的额外元素并且文件中仍然存在旧数据。它不是更新而是追加。

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

1 回答 1

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);
}       
于 2014-02-27T17:55:57.090 回答