This is my first day working with XML in the context of XPath, XQuery, and Linq, and I can’t get any of them to work for me.
My goal is to extract values for AgentGUID and PublicKey in a XML blob. The Linq query I’m using is below.
IEnumerable<string> publicKey =
from item in xDoc.Descendants("PublicKey")
select (string)item.????;
Here is the XML I’m trying to select from:
<AgentRegister xmlns="sampleURI">
server
<Servername>server</Servername>
<AgentGUID>1da3a4cf-73f2-4ee2-b8c1-cef428ad4b21</AgentGUID>
<PublicKey><RSAKeyValue><Modulus>5fuiFE74EKYUxFbSsAgeYQwyGzulQ+L1auBD1J/1gupF2s2NugpgZ6vqsi4o//vKdrKz7uhwDWeRUB5TR7hljNfOsJKbTV0sg4HywF93cyYDnfKz+2wCSxiZxAIWV8SMiui2QuD0LjbgPNGR/bBsY4GIl3eWbngjJjNEzVZq5RE=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue></PublicKey>
<ApprovedByGUID><RSAKeyValue><Modulus>5fuiFE74EKYUxFbSsAgeYQwyGzulQ+L1auBD1J/1gupF2s2NugpgZ6vqsi4o//vKdrKz7uhwDWeRUB5TR7hljNfOsJKbTV0sg4HywF93cyYDnfKz+2wCSxiZxAIWV8SMiui2QuD0LjbgPNGR/bBsY4GIl3eWbngjJjNEzVZq5RE=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue></ApprovedByGUID>
<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>2HuOAQAAxOTWz2JSq9+bJnWM8m8=</DigestValue></Reference>
</SignedInfo>
<SignatureValue>yGKq/p/JQCWSJtVKeRp4E5kHeFWVaoMFd/TbrYIm6k3nYBgr57gcEZjzvrLNsmKKaoaWspSqMzTDnrhER5AkfMi+4nhW0C6+vghNYU/jrEqT35Ov/B3aH1M41q07p3OXZc8dA1lzJ6Zh0zpx6Vd7faTfvuPqgIKmNOe07xGyP2Q=</SignatureValue>
<KeyInfo><KeyValue><RSAKeyValue>
<Modulus>5fuiFE74EKYUxFbSsAgeYQwyGzulQ+L1auBD1J/1gupF2s2NugpgZ6vqsi4o//vKdrKz7uhwDWeRUB5TR7hljNfOsJKbTV0sg4HywF93cyYDnfKz+2wCSxiZxAIWV8SMiui2QuD0LjbgPNGR/bBsY4GIl3eWbngjJjNEzVZq5RE=</Modulus>
<Exponent>AQAB</Exponent></RSAKeyValue></KeyValue></KeyInfo>
</Signature>
</AgentRegister>
How do I extract the mentioned values from this document?
I would do it using
XDocumentbut without LINQ, if you don’t expect the general structure of the document to change.If you know there’s only one instance of a given tag, it’s less trouble to just go get that instance than to call a method that selects all tags with a given name, and then extract a single value from the resulting
IEnumerable<>.However, your original code was almost right.
But then you’d have to follow that up with:
The
XElementtype defines custom conversion operators for many different primitive types. It has aValueproperty that returns a string, so you could use that and not have any cast at all if a string is what you want – but often it’s more convenient to just cast anXElementcontaining a value directly to the data type you want.Update
If you need an
XmlDocumentyou can convert anXDocumentlike this…However, it’s probably more efficient to just use an
XmlDocumentto begin with, even if it’s less convenient. One way to get the same values from anXmlDocumentis like this: