سؤال

I need to read from an XML using MSBuild. Here is the structure of the XML file

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

I am trying to use MSBuild Extensions to read the XML and store it into a Build Variable like so

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

However, the message that I output always has nothing in the variable I created. I would like the variable to be populated with "some password text"

هل كانت مفيدة؟

المحلول

You're gonna need the namespace.

Play around with this:

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

Output:

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
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top