Reputation: 723
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><EncryptionInfoResponse>
<Status>0</Status>
<Message></Message>
<ServiceID>xxxx-xxxx-xxx-xxx</ServiceID>
<KeyID>xxxx-xxxx-xxx-xxx</KeyID>
<ContentKey>KXnH9nzdDbW6kIw11yvY8A==</ContentKey>
<LicAcqURL><![CDATA[http://sldrm.licensekeyserver.com/core/rightsmanager.asmx]]></LicAcqURL>
<LicUIURL></LicUIURL>
<CustomXML><![CDATA[<CID>RMgchjQPQS+zfHTzsPGSsQ==</CID><DRMTYPE>smooth</DRMTYPE>]]></CustomXML>
<ContentID>861cc844-0f34-2f41-b37c-74f3b0f192b1</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>ZEjIHDgPQI+zffOw8ZJ0sQ==</KID><LA_URL>http://sldrm.licensekeyserver.com/core/rightsmanager.asmx</LA_URL><DS_ID>VlR7IdsIJEuRd06Laqs2jw==</DS_ID><CUSTOMATTRIBUTES xmlns=""><CID>RMgchjQPQS+zfHTzsPGSsQ==</CID><DRMTYPE>smooth</DRMTYPE></CUSTOMATTRIBUTES><CHECKSUM>/ytWga+hG9o=</CHECKSUM></DATA></WRMHEADER>]]></PRHeader>
</EncryptionInfoResponse></RequestEncryptionInfoResult>
</RequestEncryptionInfoResponse>
any help?
Upvotes: 0
Views: 355
Reputation: 141013
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;
Upvotes: 0
Reputation: 1503859
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:
XDocument.Parse
instead, so you end up with a document with an EncryptionInfoResponse
element under itDescendants("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;
Upvotes: 3