1

At the end the goal is to creat a new XML file filtered from a whole list of NODES by comparison each noed attribute value with existing values​​ list. my problem is: I can read the value of a particular node and then compare it with a dynamic variable value, But after that I want to read the tag that's wrapping it and copy it to my new XML file.

<Rule Id="2" On="a1" PL="3305" ActionResult="enabled">
<Members Operation=""><Member QId="a2" Operation="In" Attribute="checked">true
</Member></Members></Rule></Rules> 

and my code to get the values to compare is:

var ElementsList = from Elements in xdoc.Descendants("Members") 
where Elements.Element("Member").FirstAttribute.Value == "rbtn_G9000_1_11_1201__2" 
select Elements.Element("Member"); 

this will give me the code for the node itself, i need the code to get the wrapping node (Rule)...

how can i do this?

sehe
  • 374,641
  • 47
  • 450
  • 633
asafok
  • 11
  • 1

1 Answers1

0

Somewhat cleaner version of the query:

var ElementsList = 
    from els in xdoc.Descendants("Members") 
    let member = els.Element("Member")
    where member.FirstAttribute.Value == "rbtn_G9000_1_11_1201__2" 
    select member.Parent

See http://msdn.microsoft.com/en-us/library/system.xml.linq.xelement.aspx

Yet easier (untested code, guessing the intended attribute name):

// assuming some variable like:
var qid = "rbtn_G9000_1_11_1201__2"; 


var query = string.Format("/Rule/Members/Member[@QId='{0}']/..", qid);
var ElementsList = from member in 
    xdoc.XPathSelectElements(query);
sehe
  • 374,641
  • 47
  • 450
  • 633