All, I am trying to read the Role
information from Azure Cloud Service Definition
xml file orAzure Cloud Service Configure
xml file.
全部,我试图从Azure云服务定义xml文件或Azure云服务配置xml文件中读取角色信息。
But found XPath
does't work for them. Both two xml file contain namespace. By default the namespace doesn't have alias name. and If I add the alias name to it .Visual studio will alert me it is not allowed .
但发现XPath不适用于他们。两个xml文件都包含名称空间。默认情况下,命名空间没有别名。如果我为它添加别名.Visual studio会提醒我不允许。
Say your have the Definition like below. and the XmlPath is /ServiceDefinition/WebRole/@vmsize
. I tested it in a XPath online tools. and it also can't get the right value for me.
说你的定义如下。并且XmlPath是/ ServiceDefinition / WebRole / @vmsize。我在XPath在线工具中测试了它。它也无法为我找到合适的价值。
<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="test" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition" schemaVersion="2012-10.1.8">
<WebRole name="test" vmsize="Small">
<Sites>
<Site name="Web">
<Bindings>
<Binding name="Endpoint1" endpointName="HttpsEndpoint" />
<Binding name="Endpoint2" endpointName="HttpEndpoint" />
</Bindings>
</Site>
</Sites>
<Endpoints>
<InputEndpoint name="HttpsEndpoint" protocol="https" port="443" certificate="Certificate1" />
<InputEndpoint name="HttpEndpoint" protocol="http" port="80" />
</Endpoints>
<Imports>
<Import moduleName="Diagnostics" />
<Import moduleName="RemoteAccess" />
<Import moduleName="RemoteForwarder" />
</Imports>
<Certificates>
<Certificate name="Certificate1" storeLocation="LocalMachine" storeName="My" />
</Certificates>
<ConfigurationSettings>
<Setting name="LogLevel" />
</ConfigurationSettings>
</WebRole>
</ServiceDefinition>
Is there any way to make this XPath work?
有没有办法让这个XPath工作?
1 个解决方案
#1
11
When you use the Select-Xml
cmdlet with the -XPath
parameter, you also need to specify the namespace. See this article for more information:
将Select-Xml cmdlet与-XPath参数一起使用时,还需要指定命名空间。有关更多信息,请参阅此文章:
http://huddledmasses.org/xpath-and-namespaces-in-powershell/
http://huddledmasses.org/xpath-and-namespaces-in-powershell/
Here is a complete, working example
这是一个完整的,有效的例子
$XmlDocument = [xml]@'
<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="test" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition" schemaVersion="2012-10.1.8">
<WebRole name="test" vmsize="Small">
<Sites>
<Site name="Web">
<Bindings>
<Binding name="Endpoint1" endpointName="HttpsEndpoint" />
<Binding name="Endpoint2" endpointName="HttpEndpoint" />
</Bindings>
</Site>
</Sites>
<Endpoints>
<InputEndpoint name="HttpsEndpoint" protocol="https" port="443" certificate="Certificate1" />
<InputEndpoint name="HttpEndpoint" protocol="http" port="80" />
</Endpoints>
<Imports>
<Import moduleName="Diagnostics" />
<Import moduleName="RemoteAccess" />
<Import moduleName="RemoteForwarder" />
</Imports>
<Certificates>
<Certificate name="Certificate1" storeLocation="LocalMachine" storeName="My" />
</Certificates>
<ConfigurationSettings>
<Setting name="LogLevel" />
</ConfigurationSettings>
</WebRole>
</ServiceDefinition>
'@;
$XmlNamespace = @{ azure = 'http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition'; };
Select-Xml -Xml $XmlDocument -XPath '/azure:ServiceDefinition/azure:WebRole/@vmsize' -Namespace $XmlNamespace;
#1
11
When you use the Select-Xml
cmdlet with the -XPath
parameter, you also need to specify the namespace. See this article for more information:
将Select-Xml cmdlet与-XPath参数一起使用时,还需要指定命名空间。有关更多信息,请参阅此文章:
http://huddledmasses.org/xpath-and-namespaces-in-powershell/
http://huddledmasses.org/xpath-and-namespaces-in-powershell/
Here is a complete, working example
这是一个完整的,有效的例子
$XmlDocument = [xml]@'
<?xml version="1.0" encoding="utf-8"?>
<ServiceDefinition name="test" xmlns="http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition" schemaVersion="2012-10.1.8">
<WebRole name="test" vmsize="Small">
<Sites>
<Site name="Web">
<Bindings>
<Binding name="Endpoint1" endpointName="HttpsEndpoint" />
<Binding name="Endpoint2" endpointName="HttpEndpoint" />
</Bindings>
</Site>
</Sites>
<Endpoints>
<InputEndpoint name="HttpsEndpoint" protocol="https" port="443" certificate="Certificate1" />
<InputEndpoint name="HttpEndpoint" protocol="http" port="80" />
</Endpoints>
<Imports>
<Import moduleName="Diagnostics" />
<Import moduleName="RemoteAccess" />
<Import moduleName="RemoteForwarder" />
</Imports>
<Certificates>
<Certificate name="Certificate1" storeLocation="LocalMachine" storeName="My" />
</Certificates>
<ConfigurationSettings>
<Setting name="LogLevel" />
</ConfigurationSettings>
</WebRole>
</ServiceDefinition>
'@;
$XmlNamespace = @{ azure = 'http://schemas.microsoft.com/ServiceHosting/2008/10/ServiceDefinition'; };
Select-Xml -Xml $XmlDocument -XPath '/azure:ServiceDefinition/azure:WebRole/@vmsize' -Namespace $XmlNamespace;