2017-10-17 12 views
0

J'ai un XML comme ci-dessous. Je vais devoir aller chercher le titre quand le chemin est d: \ mypath. J'ai essayé ci-dessous un mais il ne donne pas comme prévu. Je voudrais l'implémenter dans LINQ to XML.Récupérer les nœuds frères 0 linq

Mon code:

XDocument xdoc = XDocument.Load(file); 
string mypath = @"D:\\Mypath"; 
var result = xdoc.Descendants("child") 
    .Where(i => (string)i.Element("content").Element("path") == mypath) 
    .Select(i => (string)i.Element("title")).FirstOrDefault(); 

Pour l'instant, j'ai terminé ma tâche à l'aide XPathSelectElement comme ci-dessous, mais je suis intéressé dans la requête LINQ:

string a = (string)xdoc.XPathSelectElement(
    "//child/content[path='" + mypath + "']/../doc_attributes/title"); 

XML Exemple:

<parent> 
    <doc> 
     <order>testorder</order> 
     <preorder>yes</preorder> 
    </doc> 
    <childs> 
     <child> 
      <doc_attributes> 
       <id>090015b3804fb931</id> 
       <title>CTA</title> 
      </doc_attributes> 
      <content> 
       <path>D:\\Mypath</path> 
      </content> 
     </child> 
    </childs> 
</parent> 

Répondre

1

Vous êtes proche, vous oubliez simplement de vérifier la propriété Value

.Where(i => i.Element("content").Element("path").Value == mypath)