Reputation: 483
Struggling in vain to extract the value of the Status descendant from an XML file generated via the Azure REST API using XDocument (LINQ to XML). No issues extracting root elements using this method:
var hsname = xmldoc.Root.Element(ns + "ServiceName").Value;
Getting the descendants is proving to be a nightmare. Abbreviated XML file below - please help :-)
-<HostedService xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.microsoft.com/windowsazure">
<Url>https://management.core.windows.net/subscriptionID/services/hostedservices/hostedservicename</Url>
<ServiceName><hostedservicename></ServiceName>
-<HostedServiceProperties>
<Description/>
<Location>South Central US</Location>
<Label>EEEEEEEEEEEEEEEEEE</Label>
</HostedServiceProperties>
-<Deployments>
-<Deployment>
<Name>DeploymentName</Name>
<DeploymentSlot>Production</DeploymentSlot>
<PrivateID>55555555555555555555</PrivateID>
<Status>Running</Status>
Upvotes: 0
Views: 164
Reputation: 1503439
You haven't shown what you've tried... but I'd expect this to be fine:
string status = (string) xmldoc.Descendants(ns + "Status").FirstOrDefault();
That will give you a null value if there are no Status
elements. You may want to use Single()
, SingleOrDefault()
etc depending on your requirements.
EDIT: Just to expand on the comment, you can make your code more robust in the face of other Status
elements like this:
string status = (string) xmldoc.Descendants(ns + "HostedService")
.Descendants(ns + "ServiceName")
.Descendants(ns + "Deployments")
.Descendants(ns + "Deployment")
.Descendants(ns + "Status")
.FirstOrDefault();
Upvotes: 3