我尝试编辑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>
答案 0 :(得分:0)
试试这个
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();
}
}
}