简体   繁体   English

LINQ 修改 XML 文档

[英]LINQ Modify XML document

I need to modify an XML file containing product sales.我需要修改一个包含产品销售的 XML 文件。 I tried to do this with LINQ, however it doesn't work.我试图用 LINQ 做到这一点,但它不起作用。 I have the following file我有以下文件

<SalesProducts>
    <ProductSale>
        <Sale type="Fixed">
        <ProductId>22</ProductId>
        <Value>50</Value>
    </ProductSale>
    <ProductSale>
        <Sale type="Discount">
        <ProductId>32</ProductId>
        <Value>33</Value>
    </Product>
</SalesProducts>

I want to add another ProductSale item after the item where product id is equal to 22.我想在产品 ID 等于 22 的项目之后添加另一个 ProductSale 项目。

I tried the following query.我尝试了以下查询。

var elProd = docX.Element("SalesProducts").
    Elements("ProductSale").
    Where(e => ((string)e.Element("ProductId")) == "22");

elProd.AddAfterSelf(
    new XElement("Sale", ""),
    new XElement("Value", "43"),
    new XElement("ProductId", "154")));

But this doesn't work.但这不起作用。 Also I can't add attribute to Sale element.此外,我无法向 Sale 元素添加属性。

Any help is highly appreciated.任何帮助都受到高度赞赏。

elProd is a Collection of XElement . elProdXElement的集合。 This is the reason you are not able to use elProd.AddAfterSelf as the method is defined for XElement and not for IEnumerable<XElement>.这就是您不能使用elProd.AddAfterSelf的原因,因为该方法是为XElement而不是IEnumerable<XElement>.

Since you are searching for specific Product, you could use Single() (Assuming there is no duplicate ProductID ) to ensure only the desired element is selected.由于您正在搜索特定的 Product,您可以使用Single() (假设没有重复的ProductID )来确保只选择所需的元素。

var elProd = docX.Element("SalesProducts")
                 .Elements("ProductSale")
                 .Single(e => ((string)e.Element("ProductId") == "22"));

Or或者

var elProd = docX.Descendants("ProductSale")
                 .Single(e => ((string)e.Element("ProductId") == "22"));

Once you have selected the desired element, you can now use AddAfterSelf to add the new Element.选择所需元素后,您现在可以使用AddAfterSelf添加新元素。 Please note you need to create an XElement ProductSale, with Sale/Value/ProductID as child.请注意,您需要创建一个XElement ProductSale,并将 Sale/Value/ProductID 作为子项。

elProd.AddAfterSelf(new XElement("ProductSale",
            new XElement("Sale", new XAttribute("type", "Type of sale")),
            new XElement("Value", "43"),
            new XElement("ProductId", "154")));

Output Sample输出样本

<SalesProducts>
  <ProductSale>
    <Sale type="Fixed"></Sale>
    <ProductId>22</ProductId>
    <Value>50</Value>
  </ProductSale>
  <ProductSale>
    <Sale type="Type of sale" />
    <Value>43</Value>
    <ProductId>154</ProductId>
  </ProductSale>
  <ProductSale>
    <Sale type="Discount"></Sale>
    <ProductId>32</ProductId>
    <Value>33</Value>
  </ProductSale>
</SalesProducts>

You could use overload of First method:您可以使用First方法的重载:

var first = xml.Elements().First(n => n.Element("ProductId").Value == "22");
first.AddAfterSelf(XElement.Parse(@"<ProductSale><Sale type='Discount'/><Value>43</Value><ProductId>154</ProductId></ProductSale>"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM