更新 XAttribute 值,其中 XAttribute Name = X

发布于 2024-11-08 11:50:50 字数 1297 浏览 0 评论 0原文

我有以下代码,它创建一个包含大量订单信息的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慕烟庭风 2024-11-15 11:50:50

像这样的东西:

var doc = XDocument.Load("FileName.xml");
var element = doc.Descendants("Order")
    .Where(arg => arg.Attribute("Id").Value == "jason")
    .Single();
element.Attribute("Quantity").Value = "3";
doc.Save("FileName.xml");

Something like this:

var doc = XDocument.Load("FileName.xml");
var element = doc.Descendants("Order")
    .Where(arg => arg.Attribute("Id").Value == "jason")
    .Single();
element.Attribute("Quantity").Value = "3";
doc.Save("FileName.xml");
凶凌 2024-11-15 11:50:50

首先,您需要搜索要更新的元素。如果找到,请进行更新。只需记住完成后将 XDocument 保存回文件即可。

XDocument doc = ...;
var jason = doc
    .Descendants("Order")
    .Where(order => order.Attribute("Id").Value == "jason") // find "jason"
    .SingleOrDefault();
if (jason != null) // if found,
{
    // update something
    jason.Attribute("Quantity").SetValue(20);
}
doc.Save(...); // save if necessary

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.

XDocument doc = ...;
var jason = doc
    .Descendants("Order")
    .Where(order => order.Attribute("Id").Value == "jason") // find "jason"
    .SingleOrDefault();
if (jason != null) // if found,
{
    // update something
    jason.Attribute("Quantity").SetValue(20);
}
doc.Save(...); // save if necessary
迷鸟归林 2024-11-15 11:50:50

由于您创建了 XML 文件,因此您知道 XML 的根元素,因此您可以使用此代码来获取所需的特定元素:

TaxonPath = XElement.Parse(xml as string);
txtSource.Text = FindGetElementValue(TaxonPath, TaxonPathElement.Source);

XElement FindGetElementValue(XElement tree,String elementname)
{
    return tree.Descendants(elementName).FirstOrDefault();
}

通过此代码,您可以获取该元素,检查其值,并根据需要更改它。

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:

TaxonPath = XElement.Parse(xml as string);
txtSource.Text = FindGetElementValue(TaxonPath, TaxonPathElement.Source);

XElement FindGetElementValue(XElement tree,String elementname)
{
    return tree.Descendants(elementName).FirstOrDefault();
}

With this, you can get the element, check its value, and change it as you desire.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文