1

How do i loop all xml attribute without knowing the element name

here is my sample xml

<dd l="11243" t="641" r="11653" b="1004">
  <para l="11276" t="768" r="11620" b="941" alignment="left" spaceBefore="79" lsp="exactly" lspExact="273" language="en">
    <ln l="11342" t="768" r="11554" b="941" baseLine="939" underlined="none" subsuperscript="none" fontSize="1250" fontFace="Times New Roman" fontFamily="roman" fontPitch="variable" spacing="19" foreColor="545766">
      <wd l="11342" t="768" r="11554" b="941">
        <ch l="11342" t="768" r="11453" b="936">4</ch>
        <ch l="11472" t="768" r="11554" b="941" conf="10">3</ch>
      </wd>
    </ln>
  </para>
</dd>

and here is my code

XDocument columndoc = XDocument.Parse(reader);
foreach (var node in columndoc.Descendants("para").ToList())
{
}

but i don't want to loop all elements and read its l, t,r,b tag.

so how can i loop using its attribute. thank you

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
pdf to image
  • 369
  • 6
  • 23
  • what is your expected result? – Jacky Dec 16 '16 at 07:02
  • Use a recursive algorithm like this posting : http://stackoverflow.com/questions/28976601/recursion-parsing-xml-file-with-attributes-into-treeview-c-sharp – jdweng Dec 16 '16 at 08:56

1 Answers1

4

Just omit the "para" argument from Descendants() - that will obtain all the descendant elements. You can then call Attributes() (which in this case is an extension method on IEnumerable<XElement>) to obtain all the attributes from all those elements.

var allAttributes = doc.Descendants().Attributes().ToList();
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194