简体   繁体   English

使用C#删除XML中的属性数组

[英]Removing an Array of attributes in XML using c#

I'm trying to edit an xml document to only contain a list of attributes I need. 我正在尝试编辑xml文档,使其仅包含我需要的属性列表。 I've created an array of attributes I need but I'm not quite sure how to filter the xml document. 我已经创建了所需的属性数组,但是我不太确定如何过滤xml文档。 Here's what I currently have: 这是我目前拥有的:

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????????
 }

Here's a sample xml with an example of what I'm trying to achieve. 这是一个示例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>

There's a nested xml file with nested nodes being named with the same name Product I need some attributes in the first, second and third nodes so a sample output would be: 有一个嵌套的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>

Try this 尝试这个

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();
        }
    }
}

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

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