写的XDocument重复的XML [英] XDocument write Duplicated xml
问题描述
我有一个方法,它加载XML到的XDocument并修改其内容,然后保存。
,当我重新加载它不过。我得到这个错误:
I have a method which load XML to a XDocument and modify its elements then save. But when I reload it. I got this error :
意外的XML声明。 XML声明必须是文件中的第一个节点,也没有空格字符被允许在它之前出现。
Unexpected XML declaration. The XML declaration must be the first node in the document, and no white space characters are allowed to appear before it.
我检查,XML和看到的XDocument没有保存改变,但创建复制并保存
I checking the XML and see that the XDocument didn't save the changed but create a duplicate and save.
它保存旧的和新的这样的示例XML:
It save the old one and the new one like this example xml :
<?xml version="1.0" encoding="UTF-8"?>
<Ungdungs>
<Ungdung>
<Name>HERE City Lens</Name>
<Id>b0a0ac22-cf9e-45ba-8120-815450e2fd71</Id>
<Path>/Icon/herecitylens.png</Path>
<Version>Unknown</Version>
<Category>HERE</Category>
<Date>Uknown</Date>
</Ungdung>
<?xml version="1.0" encoding="UTF-8"?>
<Ungdungs>
<Ungdung>
<Name>HERE City Lens</Name>
<Id>b0a0ac22-cf9e-45ba-8120-815450e2fd71</Id>
<Path>/Icon/herecitylens.png</Path>
<Version>1.0.0.0</Version>
<Category>HERE</Category>
<Date>Uknown</Date>
</Ungdung>
下面我来修改和保存XML代码:
Here the code I used to modify and save XML :
using (Stream stream = storage.OpenFile("APPSDATA.xml", FileMode.Open, FileAccess.ReadWrite))
{
//var xdoc = XDocument.Load("APPSDATA.xml");
var xdoc = XDocument.Load(stream, LoadOptions.None);
var listapp = from c in xdoc.Descendants("Ungdung") select c;
foreach (XElement app in listapp)
{
var xElement = app.Element("Name");
if (xElement != null)
progressIndicator.Text = "Checking " + xElement.Value + "...";
var element = app.Element("Id");
if (element != null)
{
var appId = element.Value;
var appVersion = await GetAppsVersion(appId);
app.Element("Version").Value = appVersion.ToString();
}
}
xdoc.Save(stream);
}
我该如何解决这个问题?
How can I solve this problem ?
推荐答案
看起来你在当前文件内容的末尾附加修改的文档。这就是为什么你以后不能再分析它。
Looks like you're appending modified document at the end of current file content. That's why you can't parse it later again.
我会分裂的阅读的和的写的部分成不同的使用
语句:
I would split read and write parts into different using
statements:
XDocument xdoc;
using (Stream stream = storage.OpenFile("APPSDATA.xml", FileMode.Open, FileAccess.Read))
{
xdoc = XDocument.Load(stream, LoadOptions.None);
}
var listapp = from c in xdoc.Descendants("Ungdung") select c;
foreach (XElement app in listapp)
{
var xElement = app.Element("Name");
if (xElement != null)
progressIndicator.Text = "Checking " + xElement.Value + "...";
var element = app.Element("Id");
if (element != null)
{
var appId = element.Value;
var appVersion = await GetAppsVersion(appId);
app.Element("Version").Value = appVersion.ToString();
}
}
using (Stream stream = storage.OpenFile("APPSDATA.xml", FileMode.Truncate, FileAccess.Write))
{
xdoc.Save(stream);
}
设置 FileMode.Truncate
在第二个语句将清除以前的文件内容,应该解决您的问题。
Setting FileMode.Truncate
on second using
statement will clear previous file content, what should fix your problem.
这篇关于写的XDocument重复的XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!