在更新Windows Phone的现有XML文件 [英] updating an existing xml file in Windows Phone
问题描述
我用下面一段代码到数据的XML文件保存在Windows手机。首先我检查目标XML文件是否在独立存储或不存在;
,如果它不存在,我创建该文件并添加所需元件的数据。如果文件存在,首先检查是否如果是的话我更新的属性值,否则添加到XML文件中的新元素的元素已经存在。
我看到的问题是如果已经元素存在,并试图更新属性(W /下面的代码) - 我看到额外的元素添加了新的数据和旧的数据仍然是文件中存在。它没有更新,而不是追加。
使用(IsolatedStorageFile存储= IsolatedStorageFile.GetUserStoreForApplication())
{
如果(storage.FileExists(文件名))
{使用
(IsolatedStorageFileStream isoStream =新IsolatedStorageFileStream(文件名,FileMode.Open,存储))
{
的XDocument文档=的XDocument。负载(isoStream);
布尔isUpdated = FALSE;
的foreach(在doc.Descendants在var中的项目(从项目(雇员)
其中item.Attribute(名)。Value.Equals(empName)
选择项).ToList ())
{
//更新现有的员工数据
//元素已经存在,需要更新现有属性
item.Attribute(名)。的SetValue(empName );
item.Attribute(ID)的SetValue(ID)。
item.Attribute(时间戳)的SetValue(时间戳)。
isUpdated = TRUE;
}
如果(!isUpdated)
{
//增加新的员工数据
doc.Element(雇员)。添加(
新XAttribute(名,empName),
新XAttribute(ID,ID),
新XAttribute(时间戳时间戳));
}
doc.Save(isoStream);
}
}
,否则
{
//创建XML文件,并使用增加员工数据
(IsolatedStorageFileStream isoStream =新IsolatedStorageFileStream(文件名,FileMode.Create ,存储))
{
的XDocument DOC =新的XDocument(新XDeclaration(1.0,UTF8,是),
新的XElement(员工,
新的XElement(雇员,
新XAttribute(名,empName),
新XAttribute(ID,ID),
新XAttribute(时间戳时间戳))) );
doc.Save(isoStream,SaveOptions.None);
}
}
}
设置打开的流位置设置为0或保存新开流中的XML文档
的XDocument DOC = NULL;
使用(IsolatedStorageFileStream isoStream =新IsolatedStorageFileStream(文件名,FileMode.Open,存储))
{$ B $(DOC)B = XDocument.Load(isoStream);
布尔isUpdated = FALSE;
的foreach(在doc.Descendants在var中的项目(从项目(雇员)
其中item.Attribute(名)。Value.Equals(empName)
选择项).ToList ())
{
//更新现有的员工数据
//元素已经存在,需要更新现有属性
item.Attribute(名)。的SetValue(empName );
item.Attribute(ID)的SetValue(ID)。
item.Attribute(时间戳)的SetValue(时间戳)。
isUpdated = TRUE;
}
如果(!isUpdated)
{
//增加新的员工数据
doc.Element(雇员)。添加(
新XAttribute(名,empName),
新XAttribute(ID,ID),
新XAttribute(时间戳时间戳));
}
//第一种方式
//isoStream.Position = 0;
//doc.Save(isoStream);
}
//或第二种方式
使用(VAR流= storage.OpenFile(文件名,FileMode.Open,FileAccess.Write))
{
doc.Save(流);
}
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);
}
}
}
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);
}
这篇关于在更新Windows Phone的现有XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!