I'm trying to edit an xml document to only contain a list of attributes I need. I've created an array of attributes I need but I'm not quite sure how to filter the xml document. 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.
<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:
<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>