更新 XAttribute 值,其中 XAttribute Name = X
我有以下代码,它创建一个包含大量订单信息的 XML 文件。我希望能够更新此 XML 文件中的条目,而不是删除所有内容并再次重新添加所有内容。
我知道我可以这样做:
xElement.Attribute(attribute).Value = value;
但这将更改与属性所保存的名称相同的每个属性。例如,当条目的 Id 等于“jason”时,如何才能仅更改某项的值?我是否需要加载 XML 文件,迭代整个文件,直到找到与我要更改的属性匹配的内容,然后更改它,然后再次保存文件?
非常感谢任何帮助/建议。
XElement xElement;
xElement = new XElement("Orders");
XElement element = new XElement(
"Order",
new XAttribute("Id", CustomId),
new XAttribute("Quantity", Quantity),
new XAttribute("PartNo", PartNo),
new XAttribute("Description", Description),
new XAttribute("Discount", Discount),
new XAttribute("Freight", Freight),
new XAttribute("UnitValue", UnitValue),
new XAttribute("LineTotal", LineTotal)
);
xElement.Add(element);
xElement.Save(PartNo + ".xml");
我的 XML 文件如下所示:
<?xml version="1.0" encoding="utf-8"?>
<Orders>
<Order Id="V45Y7B458B" Quantity="2" PartNo="5VNB98" Description="New Custom Item Description" Discount="2.00" Freight="2.90" UnitValue="27.88" LineTotal="25.09" />
<Order Id="jason" Quantity="2" PartNo="jason" Description="New Custom Item Description" Discount="2.00" Freight="2.90" UnitValue="27.88" LineTotal="25.09" />
</Orders>
I have the following code which creates an XML file with a bunch of order information. I'd like to be able to update an entry in this XML file instead of deleting everything and re-adding everything again.
I know I can do this:
xElement.Attribute(attribute).Value = value;
But that will change every attribute with the same name as attribute holds. How can I only change the value of something when the entry's Id equals "jason", for example? Would I need to Load the XML file, iterate over the entire file until it finds a match for the attribute I want to change, then change it, and then save the file again?
Any help/suggestions are greatly appreciated.
XElement xElement;
xElement = new XElement("Orders");
XElement element = new XElement(
"Order",
new XAttribute("Id", CustomId),
new XAttribute("Quantity", Quantity),
new XAttribute("PartNo", PartNo),
new XAttribute("Description", Description),
new XAttribute("Discount", Discount),
new XAttribute("Freight", Freight),
new XAttribute("UnitValue", UnitValue),
new XAttribute("LineTotal", LineTotal)
);
xElement.Add(element);
xElement.Save(PartNo + ".xml");
Here's what my XML file looks like:
<?xml version="1.0" encoding="utf-8"?>
<Orders>
<Order Id="V45Y7B458B" Quantity="2" PartNo="5VNB98" Description="New Custom Item Description" Discount="2.00" Freight="2.90" UnitValue="27.88" LineTotal="25.09" />
<Order Id="jason" Quantity="2" PartNo="jason" Description="New Custom Item Description" Discount="2.00" Freight="2.90" UnitValue="27.88" LineTotal="25.09" />
</Orders>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
像这样的东西:
Something like this:
首先,您需要搜索要更新的元素。如果找到,请进行更新。只需记住完成后将 XDocument 保存回文件即可。
First you need to search for the element that you want to update. If you find it, do the update. Just remember to save the XDocument back to the file when you're done.
由于您创建了 XML 文件,因此您知道 XML 的根元素,因此您可以使用此代码来获取所需的特定元素:
通过此代码,您可以获取该元素,检查其值,并根据需要更改它。
Since you created the XML file, you know the root element of the XML so you can use this code to get the particular element you want:
With this, you can get the element, check its value, and change it as you desire.