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

2
  • what is your expected result? Commented Dec 16, 2016 at 7:02
  • Use a recursive algorithm like this posting : stackoverflow.com/questions/28976601/… Commented Dec 16, 2016 at 8:56

1 Answer 1

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();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.