Linq to select property from XML
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 XDocument
but without LINQ, if you don't expect the general structure of the document to change.
var publicKey = (string)xDoc.Root.Element("PublicKey");
var agentGuid = (Guid)xDoc.Root.Element("AgentGUID");
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.
IEnumerable<string> publicKeys =
from item in xDoc.Descendants("PublicKey")
select (string)item;
But then you'd have to follow that up with:
string publicKey = publicKeys.First();
The XElement
type defines custom conversion operators for many different primitive types. It has a Value
property 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 an XElement
containing a value directly to the data type you want.
Update
If you need an XmlDocument
you can convert an XDocument
like this...
var doc = new XmlDocument();
doc.Load(xDoc.CreateReader());
However, it's probably more efficient to just use an XmlDocument
to begin with, even if it's less convenient. One way to get the same values from an XmlDocument
is like this:
var publicKey = doc.GetElementsByTagName("PublicKey")[0].InnerText;
var agentGuid = new Guid(doc.GetElementsByTagName("AgentGUID")[0].InnerText);
精彩评论