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
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.