我试图找出如何我可以去更新我的XML文件。我知道如何读,写,但不知道如何更新现有的记录。
I'm trying to figure out how I can go about updating my XML file. I know how to read and write, but no idea how to update an existing record.
我的XML文件是这样的:
My XML file looks like:
和我希望能够改变一个XAttribute这是已经在文件中的值。
And I'd like to be able to change the value of an XAttribute that's already in the file.
这就是我正在写文件:
XElement xElement;
xElement = new XElement("Orders");
XElement element = new XElement(
"Order",
new XAttribute("Quantity", Quantity),
new XAttribute("Part No", PartNo),
new XAttribute("Description", Description),
new XAttribute("Discount", Discount),
new XAttribute("Freight", Freight),
new XAttribute("Unit Value", UnitValue),
new XAttribute("Line Total", LineTotal)
);
xElement.Add(element);
xElement.Save("");
是否有可能做更新,或者必须首先删除现有的一个,然后用新值重新添加?
Is it possible to do updates, or must we first remove the existing one, and then re-add it with the new values?
是的,你可以更新的属性,而不删除并重新添加。简单地获得所需的 XAttribute
对象的XElement内更新它的值
属性和的XElement保存回一个文件,看变化
Yes you can update the attribute without deleting and re-adding. Simply get the desired XAttribute
object inside the XElement and update it's Value
property and save back the XElement to a file to see the changes.
xElement.Attribute("Quantity").Value = "15";