【问题标题】:Removing an Array of attributes in XML using c#使用 C# 删除 XML 中的属性数组
【发布时间】:2015-07-06 08:39:29
【问题描述】:

我正在尝试编辑 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>

【问题讨论】:

  • 你能发布一些示例 XML,即示例输入和所需输出吗?
  • 您只想更改磁盘上的字符串或文件吗?如果是这样,您可以使用正则表达式查找所有匹配项并将其替换为空字符串。如果您想过滤解析器以进行无济于事的进一步操作。
  • @DrSchizo 更新以反映我想要实现的目标。
  • 您更改的 XML 无效。有一个结束 &lt;/Values&gt; 没有相应的开始标签。您更改&lt;Value&gt;-Tag 的级别。他们现在是&lt;Product&gt; 的直接子代。正确的?如果您只需要更改字符串或文件,它看起来仍然像使用正则表达式。
  • @Verarind 我在输出上犯了一个错误。我现在已经编辑了输出。我看不出 regex 对我有什么帮助,因为我需要删除 200 多个属性。

标签: c# xml


【解决方案1】:

试试这个

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-09-30
    • 2022-09-23
    • 1970-01-01
    • 1970-01-01
    • 2016-08-07
    • 1970-01-01
    • 2020-12-26
    相关资源
    最近更新 更多