使用MSBuild读取XML文件

艾萨克·莱文

我需要使用MSBuild从XML读取。这是XML文件的结构

<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04">
   <Obj RefId="0">
      <TN RefId="0">
          <T>Selected.System.Management.Automation.PSCredential</T>
          <T>System.Management.Automation.PSCustomObject</T>
          <T>System.Object</T>
      </TN>
      <MS>
          <S N="UserName">domain\username</S>
          <S N="Password">some password text</S>
      </MS>
   </Obj>
</Objs>

我正在尝试使用MSBuild扩展来读取XML并将其存储到Build变量中,如下所示

<Target Name="LoadCredentialFile">
    <ItemGroup>
        <Namespaces Include="Mynamespace">
            <Prefix>x</Prefix>
            <Uri>http://schemas.microsoft.com/powershell/2004/04</Uri>
        </Namespaces>
    </ItemGroup>
    <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="ReadAttribute"
                                 File="$(MSBuildProjectDirectory)\DeploymentCredential.xml"
                                 XPath="/Objs/Obj/MS/S[@N='Password']"
                                 Value="$(Credential)" />

    <Message Text="Credential: $(Credential)" Importance="high" />
</Target>

但是,我输出的消息在我创建的变量中始终没有任何内容。我希望在变量中填充“一些密码文本”

手榴弹编码器

您将需要名称空间。

玩这个:

<Import Project="$(MSBuildExtensionsPath)\ExtensionPack\4.0\MSBuild.ExtensionPack.tasks"/>

<PropertyGroup>
    <!-- Always declare some kind of "base directory" and then work off of that in the majority of cases  -->
    <WorkingCheckout>.</WorkingCheckout>
</PropertyGroup>

<Target Name="AllTargetsWrapped">
    <CallTarget Targets="ReadXmlPeekValue" />
</Target>   


<Target Name="ReadXmlPeekValue">

    <ItemGroup>
        <MyNamespaces Include="peanut">
            <Prefix>peanut</Prefix>
            <Uri>http://schemas.microsoft.com/powershell/2004/04</Uri>
        </MyNamespaces>
    </ItemGroup>    

    <!--  ReadElementText   -->
    <MSBuild.ExtensionPack.Xml.XmlFile TaskAction="ReadElementText" File="$(WorkingCheckout)\Parameters.xml" 
        Namespaces="@(MyNamespaces)" XPath="//peanut:Objs/peanut:Obj/peanut:MS/peanut:S[@N='Password']">
        <Output PropertyName="MyValue1" TaskParameter="Value"/>
    </MSBuild.ExtensionPack.Xml.XmlFile>
    <Message Text="MyValue1 = $(MyValue1)"/>        

</Target>   

输出:

Target AllTargetsWrapped:
    Target ReadXmlPeekValue:
        XmlFile: .\Parameters.xml
        Read Element: //peanut:Objs/peanut:Obj/peanut:MS/peanut:S[@N='Password']
        MyValue1 = some password text

Build succeeded.
    0 Warning(s)
    0 Error(s)

Time Elapsed 00:00:00.07

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章