我正在将xml文件的内容完美地读取到附带tap事件的longlistselector中。一切都很顺利。该文件位于项目的主资产文件夹中。
现在我也想添加字符串/节点到我的简单XML,但由于某种原因,我找不到正确的语法来保存它到文件。
我的xml文件看起来像:
<?xml version="1.0" encoding="utf-8" ?>
<phrases>
<item><name>What is your name?</name></item>
<item><name>How old are you?</name></item>
</phrases>
现在我在一个按钮的点击事件中尝试了以下操作:
XDocument xDoc = XDocument.Load("phrases.xml");
var contactsElement = new XElement("item",
new XElement("name", "blalllllaaaallaala")));
xDoc.Add(contactsElement);
xDoc.Save("phrases.xml");
VS2013告诉我xDoc.Save("phrases.xml")有无效的参数。当我从文件中读取,我提供相同的路径,所以我不明白什么是预期在这里?
试试这个snippet
…
// load original XML from the stream
XDocument loadedData = XDocument.Load(stream);
// create a new parent XML structure (new root) and load the original nodes
var newXml = new XDocument(new XElement("Histories"));
newXml.Root.Add(loadedData.Root);
// create the new node
var contactsElement = new XElement("item",
new XElement("name", "blalllllaaaallaala")));
NewNode.Add(contactsElement);
// add the new node
newXml.Root.Add(NewNode);
// save the stream
newXml.Save(stream);
要了解更多,也请查看这里。