0

I have the following soap response string:

<EncryptionInfoResponse> <Status>0</Status> <Message></Message> <ServiceID>xxxxxxx</ServiceID> <KeyID>xxxxxxxxx</KeyID> <ContentKey>xxxxxxx</ContentKey> <LicAcqURL><![CDATA[http://sldrm.licensekeyserver.com/core/rightsmanager.asmx]]></LicAcqURL> <LicUIURL></LicUIURL> <CustomXML><![CDATA[<CID>pFRCPIy87oUJtOWis7IYAA==</CID><DRMTYPE>smooth</DRMTYPE>]]></CustomXML> <ContentID>xxxxxxx</ContentID> <PRHeader><![CDATA[<WRMHEADER xmlns="http://schemas.microsoft.com/DRM/2007/03/PlayReadyHeader" version="4.0.0.0"><DATA><PROTECTINFO><KEYLEN>16</KEYLEN><ALGID>AESCTR</ALGID></PROTECTINFO><KID>xvd10JPbxh5rsS27LoCIxQ==</KID><LA_URL>http://sldrm.licensekeyserver.com/core/rightsmanager.asmx</LA_URL><DS_ID>xxxxxx</DS_ID><CUSTOMATTRIBUTES xmlns=""><CID>pFRCPIy87oUJtOWis7IYAA==</CID><DRMTYPE>smooth</DRMTYPE></CUSTOMATTRIBUTES><CHECKSUM>GKaxxISZpMs=</CHECKSUM></DATA></WRMHEADER>]]></PRHeader> </EncryptionInfoResponse>

I want to get the values of "KeyID" and "ContentKey" attributes. I have tried the following below with no success, I got "Sequence contains no elements" error:

string str = Utility.ReadTextFromUrl(ConfigurationManager.AppSettings["SoapUrl"]);
XElement xdoc = XElement.Parse(str);
string result = xdoc.Descendants("EncryptionInfoResponse")
                    .Descendants("KeyID")                       
                    .First()
                    .Value;
return result;

How do I do this?

---- Edit --------------

I figured out that the problem will be the string from the URL which is a PHP script sending request to a soap service. So I wrote a Soap request with C# and was able to get response body of the soap:

using (WebResponse webResponse = webRequest.EndGetResponse(asyncResult))
{
        using (StreamReader rd = new StreamReader(webResponse.GetResponseStream()))
        {
                soapResult = rd.ReadToEnd();
        }
        System.Diagnostics.Debug.WriteLine(soapResult);
        XDocument xDoc = XDocument.Load(new StringReader(soapResult));

      var unwrappedResponse = xDoc.Descendants((XNamespace)"http://schemas.xmlsoap.org/soap/envelope/" + "Body")
                    .First()
                    .FirstNode;
       System.Diagnostics.Debug.WriteLine(unwrappedResponse);

}

But I got stuck here, I have not been able to read the KeyID and ContentKey nodes, this is the response body:

<RequestEncryptionInfoResponse xmlns="http://tempuri.org/">
  <RequestEncryptionInfoResult>&lt;EncryptionInfoResponse&gt;
  &lt;Status&gt;0&lt;/Status&gt;
  &lt;Message&gt;&lt;/Message&gt;
  &lt;ServiceID&gt;xxxx-xxxx-xxx-xxx&lt;/ServiceID&gt;
  &lt;KeyID&gt;xxxx-xxxx-xxx-xxx&lt;/KeyID&gt;
  &lt;ContentKey&gt;KXnH9nzdDbW6kIw11yvY8A==&lt;/ContentKey&gt;
  &lt;LicAcqURL&gt;&lt;![CDATA[http://sldrm.licensekeyserver.com/core/rightsmanager.asmx]]&gt;&lt;/LicAcqURL&gt;
  &lt;LicUIURL&gt;&lt;/LicUIURL&gt;
  &lt;CustomXML&gt;&lt;![CDATA[&lt;CID&gt;RMgchjQPQS+zfHTzsPGSsQ==&lt;/CID&gt;&lt;DRMTYPE&gt;smooth&lt;/DRMTYPE&gt;]]&gt;&lt;/CustomXML&gt;
  &lt;ContentID&gt;861cc844-0f34-2f41-b37c-74f3b0f192b1&lt;/ContentID&gt;
  &lt;PRHeader&gt;&lt;![CDATA[&lt;WRMHEADER xmlns="http://schemas.microsoft.com/DRM/2007/03/PlayReadyHeader" version="4.0.0.0"&gt;&lt;DATA&gt;&lt;PROTECTINFO&gt;&lt;KEYLEN&gt;16&lt;/KEYLEN&gt;&lt;ALGID&gt;AESCTR&lt;/ALGID&gt;&lt;/PROTECTINFO&gt;&lt;KID&gt;ZEjIHDgPQI+zffOw8ZJ0sQ==&lt;/KID&gt;&lt;LA_URL&gt;http://sldrm.licensekeyserver.com/core/rightsmanager.asmx&lt;/LA_URL&gt;&lt;DS_ID&gt;VlR7IdsIJEuRd06Laqs2jw==&lt;/DS_ID&gt;&lt;CUSTOMATTRIBUTES xmlns=""&gt;&lt;CID&gt;RMgchjQPQS+zfHTzsPGSsQ==&lt;/CID&gt;&lt;DRMTYPE&gt;smooth&lt;/DRMTYPE&gt;&lt;/CUSTOMATTRIBUTES&gt;&lt;CHECKSUM&gt;/ytWga+hG9o=&lt;/CHECKSUM&gt;&lt;/DATA&gt;&lt;/WRMHEADER&gt;]]&gt;&lt;/PRHeader&gt;
&lt;/EncryptionInfoResponse&gt;</RequestEncryptionInfoResult>
</RequestEncryptionInfoResponse>

any help?

2 Answers 2

3

The problem is that you're using XElement.Parse, so you're parsing the string as an element... so xdoc is already the EncryptionInfoResponse element, which doesn't have any more EncryptionInfoResponse descendants.

Options:

  • Use XDocument.Parse instead, so you end up with a document with an EncryptionInfoResponse element under it
  • Get rid of the Descendants("EncryptionInfoResponse") call, but keep the rest as it is.

Given that the KeyID is a direct child, you could make this simpler using Element(...) instead of Descendants(...).First().

XElement root = XElement.Parse(str);
string result = root.Element("KeyID").Value;
Sign up to request clarification or add additional context in comments.

5 Comments

thank for your response Jon, I tried your suggestion but I got "Object reference not set to an instance of an object" exception
@jade: Then your string isn't the string in your post, or you didn't copy my code correctly. I've just tried it, and I got the expected result.
okay, when i copied the xml string as against fetching the string from the provided url, I got an expected result. Could the problem be the returned string?
@jade: Could be. I wouldn't put it in a string to start with, to be honest - I'd got LINQ to XML to load the XML directly from the URL...
LINQ to XML, let me try that
0

You do not need to use the first descending. The rest of your code works.

string s = //Your xml here

XElement xdoc = XElement.Parse(s);
string result = xdoc
                    .Descendants("KeyID")                       
                    .First()
                    .Value;
Console.WriteLine(result);

You can also use XPath to get your node:

string s = //Your xml here
XElement xdoc = XElement.Parse(s);
var x = xdoc.CreateNavigator().SelectSingleNode("//KeyID").Value;

2 Comments

Thanks for your response. I tried your suggestion but got "Sequence contains no elements". According to @Jon, I guess the issue will have started right from how I was getting the string in the first place
I am using the XML directly into the string and it works. I guess your problem like you say is the loading.

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.