Get XML Values

So lets say I have this XML document:

<s:Envelope 
        xmlns:s="http://www.w3.org/2003/05/soap-envelope" 
        xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
        <s:Header>
            <VsDebuggerCausalityData 
                xmlns="http://schemas.microsoft.com/vstudio/diagnostics/servicemodelsink">uIDPo4tYpt6X40FEk+VSAe5mc8MAAAAAP497cBuXfk+uFIOY80O0iuLtIW56q7hLktgVYPhbnHMACQAA
            </VsDebuggerCausalityData>
            <o:Security s:mustUnderstand="1" 
                xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
                <o:BinarySecurityToken u:Id="uuid-10490fb0-8ee0-4a4c-a8db-77242c9a3b7f-2" ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">MIIF+TCCBOGgAwIBAgIQIWv3OdE866kXP/....t</o:BinarySecurityToken>
                <e:EncryptedKey Id="_0" 
                    xmlns:e="http://www.w3.org/2001/04/xmlenc#">
                    <e:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-oaep-mgf1p">
                        <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" 
                            xmlns="http://www.w3.org/2000/09/xmldsig#" />
                    </e:EncryptionMethod>
                    <KeyInfo 
                        xmlns="http://www.w3.org/2000/09/xmldsig#">
                        <o:SecurityTokenReference>
                            <o:KeyIdentifier ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509SubjectKeyIdentifier" EncodingType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-soap-message-security-1.0#Base64Binary">j0ZKFBmTz3Kj0cQ82rq63MYAR+0=</o:KeyIdentifier>
                        </o:SecurityTokenReference>
                    </KeyInfo>
                    <e:CipherData>
                        <e:CipherValue>ANCElFZ5v....==</e:CipherValue>
                    </e:CipherData>
                    <e:ReferenceList>
                        <e:DataReference URI="#_2" />
                    </e:ReferenceList>
                </e:EncryptedKey>
                <Signature 
                    xmlns="http://www.w3.org/2000/09/xmldsig#">
                    <SignedInfo>
                        <CanonicalizationMethod Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
                        <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
                        <Reference URI="#_1">
                            <Transforms>
                                <Transform Algorithm="http://www.w3.org/2001/10/xml-exc-c14n#" />
                            </Transforms>
                            <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
                            <DigestValue>akiomlPdi6j1h6r9NDqmh9G1GD0=</DigestValue>
                        </Reference>
                    </SignedInfo>
                    <SignatureValue>LIjqWD/BXsoA0XNR7hv...==</SignatureValue>
                    <KeyInfo>
                        <o:SecurityTokenReference>
                            <o:Reference ValueType="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-x509-token-profile-1.0#X509v3" URI="#uuid-10490fb0-8ee0-4a4c-a8db-77242c9a3b7f-2" />
                        </o:SecurityTokenReference>
                    </KeyInfo>
                </Signature>
            </o:Security>
        </s:Header>
        <s:Body u:Id="_1">
            <e:EncryptedData Id="_2" Type="http://www.w3.org/2001/04/xmlenc#Content" 
                xmlns:e="http://www.w3.org/2001/04/xmlenc#">
                <e:EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#aes128-cbc" />
                <e:CipherData>
                    <e:CipherValue>3kESnJnhc8K.....</e:CipherValue>
                </e:CipherData>
            </e:EncryptedData>
        </s:Body>
    </s:Envelope>

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

LVL 1
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.
0
HItesh RanaAuthor Commented:
I just tried that.  I'm getting back a count of zero.
0
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?
0
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.  
doc.GetElementsByTagName("e:EncryptedKey")[0].InnerText;
0
HItesh RanaAuthor Commented:
I think the issue is related to the namespace.  I think I need to include it in my query.
0
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 = "http://www.w3.org/2001/04/xmlenc#";

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

Open in new window

1

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
C#

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.