Requête pour récupérer les noms des nœuds de groupe
-
02-07-2019 - |
Question
Si certains fichiers XML tels que celui-ci étaient chargés dans un objet XDocument:
<Root>
<GroupA>
<Item attrib1="aaa" attrib2="000" />
</GroupA>
<GroupB>
<Item attrib1="bbb" attrib2="111" />
<Item attrib1="ccc" attrib2="222" />
<Item attrib1="ddd" attrib2="333" />
</GroupB>
<GroupC>
<Item attrib1="eee" attrib2="444" />
<Item attrib1="fff" attrib2="555" />
</GroupC>
</Root>
À quoi ressemblerait une requête pour récupérer les noms des nœuds de groupe?
Par exemple, j'aimerais qu'une requête renvoie:
GroupA
GroupB
GroupC
La solution
Quelque chose comme ça:
XDocument doc; // populate somehow
// this will give the names as XName
var names = from child in doc.Root.Elements()
select child.Name;
// if you want just the local (no-namespaces) name as a string, use this
var simpleNames = from child in doc.Root.Elements()
select child.Name.LocalName;
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow