Rellene y copie nodos XML usando Linq a XML

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)...

¿Cómo puedo hacer esto?

preguntado el 08 de noviembre de 11 a las 11:11

1 Respuestas

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

Consulte nuestra página 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);

respondido 08 nov., 11:15

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.