Linq 从 XML 选择属性

发布于 2024-10-13 12:05:36 字数 2479 浏览 2 评论 0原文

这是我第一天在 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>&lt;RSAKeyValue&gt;&lt;Modulus&gt;5fuiFE74EKYUxFbSsAgeYQwyGzulQ+L1auBD1J/1gupF2s2NugpgZ6vqsi4o//vKdrKz7uhwDWeRUB5TR7hljNfOsJKbTV0sg4HywF93cyYDnfKz+2wCSxiZxAIWV8SMiui2QuD0LjbgPNGR/bBsY4GIl3eWbngjJjNEzVZq5RE=&lt;/Modulus&gt;&lt;Exponent&gt;AQAB&lt;/Exponent&gt;&lt;/RSAKeyValue&gt;</PublicKey>
<ApprovedByGUID>&lt;RSAKeyValue&gt;&lt;Modulus&gt;5fuiFE74EKYUxFbSsAgeYQwyGzulQ+L1auBD1J/1gupF2s2NugpgZ6vqsi4o//vKdrKz7uhwDWeRUB5TR7hljNfOsJKbTV0sg4HywF93cyYDnfKz+2wCSxiZxAIWV8SMiui2QuD0LjbgPNGR/bBsY4GIl3eWbngjJjNEzVZq5RE=&lt;/Modulus&gt;&lt;Exponent&gt;AQAB&lt;/Exponent&gt;&lt;/RSAKeyValue&gt;</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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿光 2024-10-20 12:05:36

如果您不希望文档的一般结构发生变化,我会使用 XDocument 但不使用 LINQ 来完成此操作。

var publicKey = (string)xDoc.Root.Element("PublicKey");
var agentGuid = (Guid)xDoc.Root.Element("AgentGUID");

如果您知道给定标签只有一个实例,那么直接获取该实例比调用一种选择具有给定名称的所有标签的方法,然后从生成的 IEnumerable<> 中提取单个值要简单得多。 ;。

但是,您的原始代码几乎是正确的。

    IEnumerable<string> publicKeys =
        from item in xDoc.Descendants("PublicKey")
        select (string)item;

但随后您必须遵循:

    string publicKey = publicKeys.First();

XElement 类型为许多不同的基元类型定义自定义转换运算符。它有一个返回字符串的 Value 属性,因此如果您想要的是字符串,则可以使用该属性,而无需进行任何强制转换 - 但通常只强制转换 XElement 会更方便 直接包含您想要的数据类型的值。

更新

如果您需要XmlDocument,您可以像这样转换XDocument...

var doc = new XmlDocument();
doc.Load(xDoc.CreateReader());

但是,仅使用可能更有效首先使用 XmlDocument,即使它不太方便。从 XmlDocument 获取相同值的一种方法如下:

var publicKey = doc.GetElementsByTagName("PublicKey")[0].InnerText;
var agentGuid = new Guid(doc.GetElementsByTagName("AgentGUID")[0].InnerText);

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文