0

我需要使用 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 Extensions 来读取 XML 并将其存储到构建变量中,如下所示

<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>

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

4

1 回答 1

1

您将需要命名空间。

玩这个:

<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
于 2014-05-16T14:00:52.843 回答