0

It is my first time posting, and I am very new to C#, so I appreciate the help. I have so many variations of this code that are close to what I need, but I can't get on my own.

I am trying to get the contents of a sub-child in an XML sting using c#. This is the sting I receive:

<?xml version="1.0" encoding="UTF-8"?>
<activation>
    <activationOutput>
        <AID>7706a84f-5sef5-4b49-891c-98414ebb61d5</AID>
        <protectionKeyId>18305465463798407</protectionKeyId>
        <activationString>encoded xml string</activationString>// What I need
    </activationOutput>
    <activationInput>
        <activationAttribute>
              <attributeName></attributeName>
              <attributeValue></attributeValue>
              <isvPermission></isvPermission>
              <endUserPermission></endUserPermission>
        </activationAttribute>
        <activationAttribute>
           <attributeName></attributeName>
                <attributeValue></attributeValue>
        </activationAttribute>
        <comments></comments>
    </activationInput>
</activation>

I need to get the activationString value out into a string. Having the other elements would be nice, but I only care about getting into a string v2c called. The information in between the tags is another encoded XML string.

I first bring the sting into an XML element using:

XElement rootActivation = XElement.Parse(encodedActivationResponse, LoadOptions.SetLineInfo);

What has got me stumped is how to get the sub child out as a string. Using XElement element gets me the values for all of the children under ActivationOutput, but I only need the last one.

string activationOutput = rootActivation.Element("activationOutput").Value;

It sends me a long unusable string

7706a84f-5sef5-4b49-891c-98414ebb61d518305465463798407encodedxmlstring

I've also tried:

IEnumerable<XElement> activationString = rootActivation.Elements("activationString");

It looked promising, but I don't know how to get the value out as a string. It shows as

{System.Xml.Linq.Extenstions.GetDescendantsXElement>}

I was hoping that I could take the variable and push it into a string using:

string v2c = activationString.ToString;

But that does not work either

3 Answers 3

1

Your attempt

string activationOutput = rootActivation.Element("activationOutput").Value;

does give you a concatenation of all the text() nodes that are children of <activationOutput>. Obviously, that's not what you want.

To solely get the <activationString>'s text() node use

string activation = 
  (string)rootActivation.Element("activationOutput").Element("activationString").Value;

One restriction of this approach is: it only gets the first of each elements.

Sign up to request clarification or add additional context in comments.

1 Comment

This is exactly what I have been looking for. Thank you so much. I knew it was some combination of all the elements that I tried.
0

If that is the only element called activationString you could just do

string activationString = XDocument.Parse("xmlFilePathHere").Descendents("activationString").FirstOrDefault().Value;

Comments

0

In VB.Net

    Dim rootActivation As XElement
    'for testing
    rootActivation = <activation>
                         <activationOutput>
                             <AID>7706a84f-5sef5-4b49-891c-98414ebb61d5</AID>
                             <protectionKeyId>18305465463798407</protectionKeyId>
                             <activationString>encoded xml string</activationString>
                         </activationOutput>
                         <activationInput>
                             <activationAttribute>
                                 <attributeName></attributeName>
                                 <attributeValue></attributeValue>
                                 <isvPermission></isvPermission>
                                 <endUserPermission></endUserPermission>
                             </activationAttribute>
                             <activationAttribute>
                                 <attributeName></attributeName>
                                 <attributeValue></attributeValue>
                             </activationAttribute>
                             <comments></comments>
                         </activationInput>
                     </activation>

    Dim activationString As String = rootActivation.<activationOutput>.<activationString>.Value

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.