I have a complicated xml file and in different levels the following part may exist:
<ChrNote>
<note>The appropriate character is:</note>
</ChrNote>
<ChrDef>
<extension char="A">initial</extension>
<extension char="D">subsequent</extension>
<extension char="G">subsequent delayed</extension>
<extension char="S">sequel </extension>
</ChrDef>
...
and here is the code snippet that I use to build a text file:
var lv1s = from lv1 in XMLDoc.Descendants("chapter").DescendantsAndSelf()
select new
{
SChar = (string)lv1.Element("ChrNote") ?? "",
SCharDef = (string)lv1.Element("ChrDef") ?? "",
//Returns only first attribute ..?
CharLetter = (lv1.Element("ChrDef") == null ? "" :
(string)(lv1.Element("ChrDef")
.Element("extension")
.Attribute("char")) ?? "")
};
The problem is that the above query returns only the first attribute (“A”) from element “extension”. I have no experience in linq and any help will be appreciated.
Is this what you are looking for?
This will give you the following output: