提取后代 LINQ to XML
徒劳地从使用 XDocument (LINQ to XML) 通过 Azure REST API 生成的 XML 文件中提取状态后代的值。使用此方法提取根元素没有问题:
var hsname = xmldoc.Root.Element(ns + "ServiceName").Value;
事实证明,获取后代是一场噩梦。下面是缩写的 XML 文件 - 请帮忙:-)
-<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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还没有显示您尝试过的内容...但我希望这没问题:
如果没有
Status
元素,这将为您提供 null 值。根据您的要求,您可能需要使用Single()
、SingleOrDefault()
等。编辑:只是为了扩展注释,您可以使代码在面对其他
Status
元素时更加健壮,如下所示:You haven't shown what you've tried... but I'd expect this to be fine:
That will give you a null value if there are no
Status
elements. You may want to useSingle()
,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: