I'm querying a webservice in C# and it returns XML, which I've parsed into an XDocument. I remove an extraneous node that returns statistics about the service call and am left with something like this:
<xml>
<element attr1="1" attr2="2" attr3="3" attr4="4" ... attrN="N" />
<element attr1="a" attr2="b" attr3="c" ... attrN="N" />
...
<element attr1="!" attr2="?" attr3=":" attr4="" ... attrN="N />
</xml>
I'm really only interested in retrieving attr2 and attr3, so I want to remove all the other attributes to wind up with this:
<xml>
<element attr2="2" attr3="3" />
<element attr2="b" attr3="c" />
...
<element attr2="?" attr3=":" />
</xml>
However, here's the relevant bit of code I'm using and it is only removing attr1:
String[] WS_LookupFields = "attr2,attr3".Split(',');
var output = XDocument.Parse(WS_Output_Raw);
output.Descendants("system").Remove(); //removes query statistics
foreach (XAttribute attribute in output.Descendants("element").Attributes())
{
if (!Array.Exists<String>(WS_LookupFields, fieldname => attribute.Name == fieldname)) attribute.Remove();
}
The problem is that the foreach only returns attr1 and not attr1 through attrN. Any suggestions?