1

My powershell code actually reads data from an XML and stores the specific data to a csv file. The XML file somewhat looks like below:

<?xml version="1.0" encoding="utf-8"?>
<Report Version="10.0">
<Targets>
<Target Name="\\bin\testBusiness.dll">
<Modules>
<Module Name="testing.dll" AssemblyVersion="1.0.1003.312" FileVersion="1.0.0.0">
<Metrics>
<Metric Name="Maintainability" Value="78" />
</Metrics>
</Module>
</Modules>
</Target>
</Targets>
</Report>

I need to extract only the "testing.dll" from the above XML code. The code I am using to do so is as below:

$testDLL = [regex]::matches($xmlfile[5], '<mod name=".*" ')[0].value -replace '<mod name="(.*)" ','$1'
#the above code line gets even the AssemblyVersion 
$testAssembver =  [regex]::matches($xmlfile[5], 'AssemblyVersion=".*" ')[0].value -replace 'AssemblyVersion=".*" ','$1'  

I don't need AssemblyVersion to be concatenated to the "testing.dll (mod name in xml)" from the XML code.

Currently I get something like:

 testing.dll" AssemblyVersion=1.0.1000.112" 

I just need testing.dll, everything thereafter should be ommitted.

Please help.

Thanks, Ashish

2 Answers 2

3

I don't think that regular expression is the best way to parse XML. Perhaps ou'd better use XMLDocument.

Using a better XML document :

<Dumy>
<Report Version="10.0"></Report>
<Goals>
  <Name>"\\somepath\path.dll"</Name>
  <Mods>
    <Mod Name="testing.dll" AssemblyVersion="1.0.1000.112" FileVersion="1.0.0.1"></Mod>
  </Mods>
</Goals>
</Dumy>

You can find your data like this :

$data = [XML](Get-Content "C:\temp\test.xml")
$data.Dumy.Goals.Mods.Mod.Name
4
  • Thanks for your effor JPBlanc. But is there a way out using -replace twice or something else. I am getting the testing.dll but alongwith the AsemblyVersion which I need to ommit.
    – ashish g
    Commented Oct 9, 2012 at 11:58
  • And I actually get the field as blank when I use your code. :(
    – ashish g
    Commented Oct 9, 2012 at 12:08
  • Can you extract a cohérent part of your exact XML code (well formated) ?
    – JPBlanc
    Commented Oct 9, 2012 at 14:29
  • @hashish, JPBlanc used a modified version of your XML. Did you adapt the code to your situation? Commented Oct 10, 2012 at 9:57
0

Use XML as correctly suggested by JPBlanc. Then use XPath. Example, to extract the wanted value:

$data.selectnodes("//Modules/Module/@Name")
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.