Ben
Ben

Reputation: 1321

LINQ to Xml Select Element

I'm having some real issue's getting an element value from my XML. I've always seemed to have problems with XML. Code that works perfectly for others wont for mine, I must be missing something but i have no idea what.

I have the following XML:

    <?xml version="1.0" encoding="UTF-8"?>
<license xmlns="http://http://lilleker-it.co.uk/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <SID>S-1-5-21-1231312-12344234-12312312</SID>
   <Email>[email protected]</Email>
   <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
      <SignedInfo>
         <CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315" />
         <SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#rsa-sha1" />
         <Reference URI="">
            <Transforms>
               <Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
            </Transforms>
            <DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
            <DigestValue>aZd9Jsbqif+8KKRYuaKzuXTLPGA=</DigestValue>
         </Reference>
      </SignedInfo>
      <SignatureValue>PB613rI/Nh4E3LBY0pG52vTCsH6dple2nXXjnnHhpsW2ZOG6lcMPmPmQWAzZVEPDPACg44Tn37hBoRBoRZ4T98qwB5tRfDRD9jXgcC912334dfDFADdcdkoYXTSiVaVWsUe4x3T665VKf8Dej2e9bFXOuhCegXA5BP1Jeak=</SignatureValue>
   </Signature>
</license>

What i need is to select the value of Email and SID, however i just cannot seem to get it to work, i have tried the following code:

var query = doc.Descendants("license").Select(s => new
                {
                    EMAIL = s.Element("Email").Value,
                    SID = s.Element("SID").Value
                }).ToList();
                string e = query[0].EMAIL;
                string id = query[0].SID;

As well as:

string e = doc.Root.Element("license")
              .Elements("Email")
              .SingleOrDefault()
              .Value;

None of these returns a value, always null and i cannot see why.

Upvotes: 0

Views: 96

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1499770

Both of your sample code snippets will fail because they're looking for elements in no namespace. Your second sample will also fail because you're looking for a license element under the root element, when in fact it is the root element.

You need to use the right namespace. For example, to fix your first query (because that looks like it's doing more of what you want) you'd use:

XNamespace ns = "http://http://lilleker-it.co.uk/";
var query = doc.Descendants(ns + "license").Select(s => new
                {
                    EMAIL = s.Element(ns + "Email").Value,
                    SID = s.Element(ns + "SID").Value
                }).ToList();
string e = query[0].EMAIL;
string id = query[0].SID;

Although personally I'd handle it rather differently:

XNamespace ns = "http://http://lilleker-it.co.uk/";
string e = (string) doc.Root.Element(ns + "Email");
string id = (string) doc.Root.Element(ns + "SID");

There's no need to use a LINQ query at all here - just find the first element under the root with the right name, and convert it to a string. That will give you a null value for each of s or id if the corresponding element is missing, so you can detect that easily and handle it appropriately.

Upvotes: 1

Related Questions