Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3790032
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T12:15:03+00:00 2026-05-19T12:15:03+00:00

This is my first day working with XML in the context of XPath, XQuery,

  • 0

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>&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 mentioned values from this document?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T12:15:03+00:00Added an answer on May 19, 2026 at 12:15 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right, so this is my first day using XML. I'm not creating the XML,
I'm using this line to get the beginning time of the first day of
This is my first question and first day in Linq so bit difficult day
I've been trying to get this working correctly all day, its nearly complete just
I've been working at this all day, and I'm really close but just can't
This is the first time I am working this intensly with XML in Java.
I am a python programmer, and this is my first day working with R.
Alright this is my first day with JQuery so have fun with these functions
I am currently getting first day Of this week and last week values with
I've spent half day trying to figure out this and finally I got working

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.