C #: obtenha conteúdo do nó XML com todas as tags pai
Há um arquivo XML:
<favorites>
<movies>
<movie title="The Godfather" year="1974" />
<movie title="The Terminator" year="1984" />
<movie title="Dark Knight" year="2008" />
</movies>
<books>
<book title = "1984" author="George Orwell" />
<book title = "Robinson Crusoe"" author="Daniel Defoe"/>
<book title = "Frankenstein" author="Mary Shelly" />
</books>
<music>
<artist title = "Beatles" genre="rock" />
<artist title = "Queen" genre="rock" />
<artist title = "Metallica" rock="heavy metal" />
</music>
</favorites>
Eu preciso filtrar um nó específico (música)com todos os pais e filhos nós. O resultado deve ser:
<favorites>
<music>
<artist title="Beatles" genre="rock" />
<artist title="Queen" genre="rock" />
<artist title="Metallica" rock="heavy metal" />
</music>
</favorites>
Encontrei uma solução para filtrar XML usando System.XML e LINQ:
XDocument xdoc = XDocument.Parse(xmlString);
xdoc.Descendants()
.Where(x => x.Name != "favorites" & x.Name != "music" & x.Name != "artist")
.Remove();
Console.WriteLine(xdoc.ToString());
A amostra de trabalho está aqui:https://dotnetfiddle.net/NnA4h7#
O problema é que eu tenho que especificar todos os nós pai e filho na minha consulta.
Existe uma maneira de obter o mesmo resultado especificando apenas um nó desejado (filmes, livros, música)?