1

I have an xml file and I need to read only a particular substring from the main string. The xml file looks like below:

<?xml version="1.0" encoding="utf-8"?>
<Report Version="10.0">
<Entities>
<Entity Name="\\sharing\Data\SB\**appname**\wcf\Utilitysvc\bin\svcUtility.Host.dll">
<Mods>
<Mod Name="svcUtility.Host.dll" AssemblyVersion="1.0.2000.001">
<Fields>
<Field Name="TIndex" Value="100" />            
<Field Name="Vindex" Value="200" />
</Fields>
</Mod>
</Mods>
</Entity>
</Entities>
</Report>

The main string in this xml is -

<Entity Name="\\sharing\Data\SB\**appname**\wcf\Utilitysvc\bin\svcUtility.Host.dll">

And I need to print only the "appname" from it.

What condition logic can I use to print this using regex in powershell? And it need not be \wcf after the appname always.. it can be anything based on the dll path. For Eg, it can be like:

<Entity Name="\\sharing\Data\*SB*\**appname**\**Web**\Utilitysvc\bin\svcUtility.Host.dll">

or

<Entity Name="\\sharing\Data\*SB*\*DEVCS*\**appname**\**junk**\Utilitysvc\bin\svcUtility.Host.dll">

Can I have a generic select -string way? need to test this as well..

Thanks,
Ashish

4

2 回答 2

2

这是一种方式:

$xml = [xml](get-content .\my.xlm )

 ($xml.Report.Entities.Entity.name | 
% { [regex]::matches($_, 'SB\\(.*)\\wcf') } |
 select -expand groups)[1].value

没有 [regex] .net 方法:

($xml.Report.Entities.Entity.name |
select-string 'SB\\(.*)\\wcf' -AllMatches | select -ExpandProperty matches |
select -ExpandProperty groups)[1].value

编辑:

根据您的最后评论尝试此模式:

 ($xml.Report.Entities.Entity.name |
    select-string '(?<=\\Data\\.*\\)[^\\]*' -AllMatches |
    select -ExpandProperty matches |
    select -ExpandProperty groups)[0].value
于 2012-10-23T06:14:27.727 回答
1

您可以在没有正则表达式的复杂性的情况下做到这一点,拆分路径并获取第 5 个元素(听起来像电影名称):

[xml]$xml = @"
<?xml version="1.0" encoding="utf-8"?>
<Report Version="10.0">
<Entities>
<Entity Name="\\sharing\Data\SB\**appname**\wcf\Utilitysvc\bin\svcUtility.Host.dll">
<Mods>
<Mod Name="svcUtility.Host.dll" AssemblyVersion="1.0.2000.001">
<Fields>
<Field Name="TIndex" Value="100" />            
<Field Name="Vindex" Value="200" />
</Fields>
</Mod>
</Mods>
</Entity>
</Entities>
</Report>
"@

$xml.Report.Entities.Entity.Name.split('\')[5]

**appname**
于 2012-10-23T06:12:20.310 回答