读取包含命名空间PowerShell的XML文件

 男人还是闷骚点好 发布于 2023-02-07 10:22

全部,我试图RoleAzure Cloud Service Definitionxml文件或Azure Cloud Service Configurexml文件中读取信息.

但发现XPath对他们没有用.两个xml文件都包含名称空间.默认情况下,命名空间没有别名.如果我为它添加别名.Visual studio会提醒我不允许.

说你的定义如下.和XmlPath是/ServiceDefinition/WebRole/@vmsize.我在XPath在线工具中测试了它.它也无法为我找到合适的价值.



  
    
      
        
          
          
        
      
    
    
      
      
    
    
      
      
      
    
    
      
    
    
      
    
  

有没有办法使这个XPath工作?

1 个回答
  • When you use the Select-Xml cmdlet with the -XPath parameter, you also need to specify the namespace. See this article for more information:

    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;
    

    2023-02-07 10:23 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有