Linq 从 XML 选择属性
这是我第一天在 XPath、XQuery 和 Linq 上下文中使用 XML,但我无法让它们中的任何一个为我工作。
我的目标是在 XML blob 中提取 AgentGUID 和 PublicKey 的值。我正在使用的 Linq 查询如下。
IEnumerable<string> publicKey =
from item in xDoc.Descendants("PublicKey")
select (string)item.????;
以下是我尝试从中选择的 XML:
<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 上述值从此文档中?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不希望文档的一般结构发生变化,我会使用 XDocument 但不使用 LINQ 来完成此操作。
如果您知道给定标签只有一个实例,那么直接获取该实例比调用一种选择具有给定名称的所有标签的方法,然后从生成的 IEnumerable<> 中提取单个值要简单得多。 ;。
但是,您的原始代码几乎是正确的。
但随后您必须遵循:
XElement
类型为许多不同的基元类型定义自定义转换运算符。它有一个返回字符串的Value
属性,因此如果您想要的是字符串,则可以使用该属性,而无需进行任何强制转换 - 但通常只强制转换XElement 会更方便
直接包含您想要的数据类型的值。更新
如果您需要
XmlDocument
,您可以像这样转换XDocument
...但是,仅使用
可能更有效首先使用 XmlDocument
,即使它不太方便。从XmlDocument
获取相同值的一种方法如下:I would do it using
XDocument
but 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
XElement
type defines custom conversion operators for many different primitive types. It has aValue
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 anXElement
containing a value directly to the data type you want.Update
If you need an
XmlDocument
you can convert anXDocument
like this...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 anXmlDocument
is like this: