MSBuild Looping
-
Monday, July 09, 2012 7:38 AM
Hi,
In my project file, I am reading XML tags of a file like this:
<Target Name="ReadXML">
<Message Text="Calling ReadXML"/><MSBuild.ExtensionPack.Xml.XmlFile TaskAction="ReadElementText" File="$(SolutionRoot)\Preferences\TestCaseResult.xml" XPath="/TestCases/Case[1]/Testcase">
<Output PropertyName="TestCase1id" TaskParameter="Value"/>
</MSBuild.ExtensionPack.Xml.XmlFile><MSBuild.ExtensionPack.Xml.XmlFile TaskAction="ReadElementText" File="$(SolutionRoot)\Preferences\TestCaseResult.xml" XPath="/TestCases/Case[1]/Description">
<Output PropertyName="TestCase1desc" TaskParameter="Value"/>
</MSBuild.ExtensionPack.Xml.XmlFile><MSBuild.ExtensionPack.Xml.XmlFile TaskAction="ReadElementText" File="$(SolutionRoot)\Preferences\TestCaseResult.xml" XPath="/TestCases/Case[1]/Result">
<Output PropertyName="TestCase1res" TaskParameter="Value"/>
</MSBuild.ExtensionPack.Xml.XmlFile><MSBuild.ExtensionPack.Xml.XmlFile TaskAction="ReadElementText" File="$(SolutionRoot)\Preferences\TestCaseResult.xml" XPath="/TestCases/Case[2]/Testcase">
<Output PropertyName="TestCase2id" TaskParameter="Value"/>
</MSBuild.ExtensionPack.Xml.XmlFile><MSBuild.ExtensionPack.Xml.XmlFile TaskAction="ReadElementText" File="$(SolutionRoot)\Preferences\TestCaseResult.xml" XPath="/TestCases/Case[2]/Description">
<Output PropertyName="TestCase2desc" TaskParameter="Value"/>
</MSBuild.ExtensionPack.Xml.XmlFile>
<MSBuild.ExtensionPack.Xml.XmlFile TaskAction="ReadElementText" File="$(SolutionRoot)\Preferences\TestCaseResult.xml" XPath="/TestCases/Case[2]/Result">
<Output PropertyName="TestCase2res" TaskParameter="Value"/>
</MSBuild.ExtensionPack.Xml.XmlFile><MSBuild.ExtensionPack.Xml.XmlFile TaskAction="ReadElementText" File="$(SolutionRoot)\Preferences\TestCaseResult.xml" XPath="/TestCases/Case[3]/Testcase">
<Output PropertyName="TestCase3id" TaskParameter="Value"/>
</MSBuild.ExtensionPack.Xml.XmlFile>
<MSBuild.ExtensionPack.Xml.XmlFile TaskAction="ReadElementText" File="$(SolutionRoot)\Preferences\TestCaseResult.xml" XPath="/TestCases/Case[3]/Description">
<Output PropertyName="TestCase3desc" TaskParameter="Value"/>
</MSBuild.ExtensionPack.Xml.XmlFile>
<MSBuild.ExtensionPack.Xml.XmlFile TaskAction="ReadElementText" File="$(SolutionRoot)\Preferences\TestCaseResult.xml" XPath="/TestCases/Case[3]/Result">
<Output PropertyName="TestCase3res" TaskParameter="Value"/>
</MSBuild.ExtensionPack.Xml.XmlFile>and so on.. As you can see for every Case I have to rewrite the same lines of code again. Is it possible to use a loop here whereby I can simply replace the Case[no] with a variable.
Please help.
Thanks,
All Replies
-
Tuesday, July 10, 2012 2:12 AMModerator
Hi sbrars,
You can add the variable to itemgroup like:
<ItemGroup>
<Xps Include="/TestCases/Case[1]">
<CaseNo>1</CaseNo>
</Xps>
<Xps Include="/TestCases/Case[2]">
<CaseNo>2</CaseNo>
</Xps>
<Xps Include="/TestCases/Case[3]">
<CaseNo>3</CaseNo>
</Xps>
...
</ItemGroup >
And you can modify your codes like:<MSBuild.ExtensionPack.Xml.XmlFile TaskAction="ReadElementText" File="$(SolutionRoot)\Preferences\TestCaseResult.xml" XPath="/TestCases/Case[%(Xps.CaseNo)]/Testcase">
<Output PropertyName="TestCase1id" TaskParameter="Value"/>
</MSBuild.ExtensionPack.Xml.XmlFile>
<MSBuild.ExtensionPack.Xml.XmlFile TaskAction="ReadElementText" File="$(SolutionRoot)\Preferences\TestCaseResult.xml" XPath="/TestCases/Case[%(Xps.CaseNo)]/Description">
<Output PropertyName="TestCase1desc" TaskParameter="Value"/>
</MSBuild.ExtensionPack.Xml.XmlFile>
<MSBuild.ExtensionPack.Xml.XmlFile TaskAction="ReadElementText" File="$(SolutionRoot)\Preferences\TestCaseResult.xml" XPath="/TestCases/Case[%(Xps.CaseNo)]/Result">
<Output PropertyName="TestCase1res" TaskParameter="Value"/>
</MSBuild.ExtensionPack.Xml.XmlFile>
If you have any concerns about this issue, please feel free to let us know.
Best regards,
Ego [MSFT]
MSDN Community Support | Feedback to us
- Marked As Answer by Ego JiangMicrosoft Contingent Staff, Moderator Friday, July 27, 2012 8:35 AM
-
Tuesday, July 10, 2012 6:52 AM
Hi,
Thanks for your reply. My xml file from which I am reading the tags has following structure:
<?xml version="1.0" encoding="UTF-16" ?><Testcase>ID="108160"</Testcase><Description>Verify "AppStudioCoreXT" is getting loaded</Description><Result /></Case><Testcase>ID="108168"</Testcase><Description>Verify "AppStudioUIXT" is getting loaded</Description><Result /></Case><Testcase>ID="108177"</Testcase><Description>Verify HTML5 Palette on Windows</Description><Result>FAIL</Result></Case>
</TestCases>
This means I have to define all my testcases which are already there in xml into <ItemGroup> tag. Also If I have 50 testcases in future or maybe more, all of them need to be explicitly specified in <ItemGroup> tag.
Please advise.
Thanks,
Suman
-
Wednesday, July 11, 2012 9:09 AMModerator
Hi sbrars,
If you want to run msbuild by this way, yes, you will add them to itemgroup.
I will do some researches. If I find a better way, I will post here immediately.
Best regards,
Ego [MSFT]
MSDN Community Support | Feedback to us
-
Wednesday, July 11, 2012 9:50 AM
Hi,
I would really appreciate if you can help me with a better solution otherwise writing all of the xml file again into the ItemGroup tag would not make sense.
Thanks for your help.
Thanks,
Suman

