user2948533
user2948533

Reputation: 1173

How to iterate throgh XAttribute of XElement in C#?

I have the following XElement structure:

XElement xmleElement=

<portal>
 <form patientid="72615" consentformid="430" appointmentid="386919"   actiontype="3">
   <signatures>
     <signature signatureid="858" encodedsignature="rkJggg==" />
   <signature signatureid="859" encodedsignature="" />
    </signatures>
  </form>
</portal>

Now I want to iterate through this elements each signature and get each encodedsignature XAttribute. Basically want each portal/form/signatures/sugnature[encodedsignature] attribute using some foreach kind of iterator. Any help will be highly appretiated. Thanks in advance.

Upvotes: 1

Views: 89

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1503290

Sounds like you want something like:

var encodedSignatures = doc.Descendants("signature")
                           .Select(x => x.Attribute("encodedSignature").Value;

Or to be more explicit about the path:

var encodedSignatures = doc.Root
                           .Element("form")
                           .Element("signatures")
                           .Elements("signature")
                           .Select(x => x.Attribute("encodedSignature").Value;

In either case, you can then iterate using foreach - encodedSignatures will just be an IEnumerable<string>.

Upvotes: 2

Related Questions