2012-10-09 5 views
1

내 powershell 코드는 실제로 XML에서 데이터를 읽고 특정 데이터를 csv 파일에 저장합니다. 내가 추출물 단지 "testing.dll"위의 XML 코드에서 필요Powershell 스크립트를 사용하여 주 문자열에서 부분 문자열을 제거하려면 어떻게해야합니까?

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

: 아래와 같은 는 XML 파일이 다소 보인다. 내가 그렇게 사용하고 코드는 다음과 같습니다 :

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

내가 AssemblyVersion 필요하지 않습니다는 XML 코드에서 "testing.dll (XML에 모드의 이름)"에 연결될 수 할 수 있습니다.

testing.dll" AssemblyVersion=1.0.1000.112" 

난 그냥 testing.dll 필요, 모든 것이 그 후 ommitted해야합니다

현재 내가 좋아하는 뭔가를 얻을.

도와주세요.

덕분에, 인 Ashish

답변

3

나는 정규 표현식은 XML을 구문 분석하는 가장 좋은 방법이라고 생각하지 않습니다. 아마도 XMLDocument를 사용하는 것이 좋습니다. 더 나은 XML 문서를 사용

는 :

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

이 같은 데이터를 찾을 수 있습니다

$data = [XML](Get-Content "C:\temp\test.xml") 
$data.Dumy.Goals.Mods.Mod.Name 
+0

감사의 말 JPBlanc. 그러나 -replace를 두 번 사용하거나 다른 것을 사용하는 방법이 있습니까? 나는 testing.dll을 얻고 있지만 함께 할 필요가있는 AsemblyVersion을 가지고있다. –

+0

그리고 코드를 사용할 때 실제로 필드를 비워 둡니다. ( –

+0

정확하게 XML 코드의 일부분을 추출 할 수 있습니까? 잘 만들었습니까? – JPBlanc

0

사용 XML은 제대로 JPBlanc에 의해 제안했다. 그런 다음 XPath를 사용하십시오. 예 : 원하는 값 추출 :

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

JPBlanc과 Empo에게 감사드립니다. 일했다. :) –

관련 문제