Get XML Values

So lets say I have this XML document:

            <o:Security s:mustUnderstand="1" 
                <o:BinarySecurityToken u:Id="uuid-10490fb0-8ee0-4a4c-a8db-77242c9a3b7f-2" ValueType="" EncodingType="">MIIF+TCCBOGgAwIBAgIQIWv3OdE866kXP/....t</o:BinarySecurityToken>
                <e:EncryptedKey Id="_0" 
                    <e:EncryptionMethod Algorithm="">
                        <DigestMethod Algorithm="" 
                            xmlns="" />
                            <o:KeyIdentifier ValueType="" EncodingType="">j0ZKFBmTz3Kj0cQ82rq63MYAR+0=</o:KeyIdentifier>
                        <e:DataReference URI="#_2" />
                        <CanonicalizationMethod Algorithm="" />
                        <SignatureMethod Algorithm="" />
                        <Reference URI="#_1">
                                <Transform Algorithm="" />
                            <DigestMethod Algorithm="" />
                            <o:Reference ValueType="" URI="#uuid-10490fb0-8ee0-4a4c-a8db-77242c9a3b7f-2" />
        <s:Body u:Id="_1">
            <e:EncryptedData Id="_2" Type="" 
                <e:EncryptionMethod Algorithm="" />

Open in new window

I want to pragmatically get two values
1)  Header > Security > EncryptedKey > CipherData > CipherValue. So it would be ANCElFZ5v....==
2)  Body > EncryptedData > CipherData > CipherValue. So it would be 3kESnJnhc8K.....

I have been trying XML to LINQ but can't seem to figure it out.  

I have been trying various thing like.  But it seems to be not working.
XDocument file = XDocument.Load(stream);
var encyptKey = file.Root.Elements("Security").Elements("CipherValue").ToList();

Open in new window

HItesh RanaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Kyle AbrahamsSenior .Net DeveloperCommented:
are you missing some paths??

var headerEncyptKey = file.Root.Elements("Header").Elements("Security").Elements("EncryptedKey").Elements("CipherData").Elements("CipherValue").ToList();

Open in new window

or use Descendants:

var encyptKeys =  from c in file.Descendants("CipherValue") select c;

Open in new window

 <--- Will return all descendants.
HItesh RanaAuthor Commented:
I just tried that.  I'm getting back a count of zero.
Kyle AbrahamsSenior .Net DeveloperCommented:
Can you ensure you're reading from the stream correctly?  Does file have anything after you load it?

Also did you try using descendants?
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

HItesh RanaAuthor Commented:
I look at the contents of the file contents and I see the whole document as a string.  

I tried
var encyptKeys = from c in file.Descendants("CipherValue") select c;

then do
encyptKeys.Count() and I see zero.

Can I use GetElementsByTagName?  I was going to use that.  I get getting more than I wanted when I ran this query.  
HItesh RanaAuthor Commented:
I think the issue is related to the namespace.  I think I need to include it in my query.
Fernando SotoRetiredCommented:
Hi HItesh Rana;

You are correct, it is an XNamespace issue. Try the code like this.
var xdoc = XDocument.Load(stream);
XNamespace ns_e = "";

var encyptKey = xdoc.Root.Descendants(ns_e + "CipherValue").ToList();

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.