使用c#在XML中删除属性数组

本文关键字:删除 属性 数组 XML 使用 | 更新日期: 2023-09-27 18:01:21

我想编辑一个xml文档,只包含我需要的属性列表。我已经创建了一个需要的属性数组,但我不太确定如何过滤xml文档。以下是我目前的内容:

var desiredIds = new[] { "fooo1","attribute2", "attribute3" };
var fullAttributeList = xml.Descendants("Value").Attributes("AttributeID");
//Exception list.... 
var rejectThis = rangeProducts.Descendants("Value").Where(y => desiredIds.Contains((string)y.Attribute("AttributeID")));
foreach (var item in rangeProducts.Descendants("Value").Except(rejectThis))
 {
   item.RemoveAttributes(); //nope.... 
   //What now????????
 }

下面是一个示例xml,其中包含我试图实现的示例。

<Product ID="Sample A" UserTypeID="TYPE_PRD_RANGE">
  <Values AttributeId = "AAAAAA">
    <Value AttributeId = "BBBBBB">Value1</Value>
    <Value AttributeId = "CCCCCC">Value2</Value>
    <Value AttributeId = "DDDDDD">Value3</Value>
    <Value AttributeId = "EEEEE">Value4</Value>
  </Values>
  <Product ID="Sample A_1" UserTypeID="SUB_RANGE">
    <Values AttributeId = "ZZZZZZ">
      <Value AttributeId = "YYYYYY">Value1</Value>
      <Value AttributeId = "CCCCCC">Value2</Value>
      <Value AttributeId = "DDDDDD">Value3</Value>
      <Value AttributeId = "BBBBBB">Value4</Value>
    </Values>
  </Product>
  <Product ID="Sample A_1_1" UserTypeID="ITEM">
    <Values AttributeId = "12345">
      <Value AttributeId = "N12345">Value1</Value>
      <Value AttributeId = "A12345">Value2</Value>
      <Value AttributeId = "C12345">Value3</Value>
      <Value AttributeId = "F12345">Value4</Value>
    </Values>
  </Product>    
</Product>

有一个嵌套的xml文件,嵌套的节点被命名为相同的名称Product我需要一些属性在第一,第二和第三个节点,所以一个示例输出将是:

<Product ID="Sample A" UserTypeID="TYPE_PRD_RANGE">
  <Value AttributeId = "DDDDDD">Value3</Value>
  <Value AttributeId = "EEEEE">Value4</Value>
  <Product ID="Sample A_1" UserTypeID="SUB_RANGE">
    <Value AttributeId = "BBBBBB">Value4</Value>    
  </Product>
  <Product ID="Sample A_1_1" UserTypeID="ITEM">
    <Value AttributeId = "F12345">Value4</Value>
  </Product>
</Product>

使用c#在XML中删除属性数组

试试这个

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
namespace ConsoleApplication34
{
    class Program
    {
        static void Main(string[] args)
        {
            string input =
              "<Product ID='"Sample A'" UserTypeID='"TYPE_PRD_RANGE'">" +
                "<Values AttributeId = '"AAAAAA'">" +
                  "<Value AttributeId = '"BBBBBB'">Value1'"</Value>" +
                  "<Value AttributeId = '"CCCCCC'">Value2'"</Value>" +
                  "<Value AttributeId = '"DDDDDD'">Value3'"</Value>" +
                  "<Value AttributeId = '"EEEEE'">Value4'"</Value>" +
                "</Values>" +
                "<Product ID='"Sample A_1'" UserTypeID='"SUB_RANGE'">" +
                  "<Values AttributeId = '"ZZZZZZ'">" +
                    "<Value AttributeId = '"YYYYYY'">Value1'"</Value>" +
                    "<Value AttributeId = '"CCCCCC'">Value2'"</Value>" +
                    "<Value AttributeId = '"DDDDDD'">Value3'"</Value>" +
                    "<Value AttributeId = '"BBBBBB'">Value4'"</Value>" +
                  "</Values>" +
                "</Product>" +
                "<Product ID='"Sample A_1_1'" UserTypeID='"ITEM'">" +
                  "<Values AttributeId = '"12345'">" +
                    "<Value AttributeId = '"N12345'">Value1'"</Value>" +
                    "<Value AttributeId = '"A12345'">Value2'"</Value>" +
                    "<Value AttributeId = '"C12345'">Value3'"</Value>" +
                    "<Value AttributeId = '"F12345'">Value4'"</Value>" +
                  "</Values>" +
                "</Product>" +
              "</Product>";
              XDocument doc = XDocument.Parse(input);
              var results =doc.Descendants("Product").Select(x => new {
                  id = x.Attribute("ID").Value,
                  valuesId = x.Element("Values").Attribute("AttributeId").Value,
                  values = x.Element("Values").Descendants("Value").Select(y => new {
                     valueId = y.Attribute("AttributeId").Value,
                     value = (string)y
                  }).ToList()
              }).ToList();
        }
    }
}