简体   繁体   English

PowerShell - 解析 XML 字符串

[英]PowerShell - Parse string that is XML

I am running a SQL query and one of the fields returned contains a string that is formatted as XML.我正在运行 SQL 查询,返回的字段之一包含格式为 XML 的字符串。 The string returned is a single line containing all the elements, I added carriage returns for readability.返回的字符串是包含所有元素的一行,我添加了回车符以提高可读性。 I have tried converting the string to XML using "convertto-xml" and it seems to create an XML object, but I cannot seem to parse it.我尝试使用“convertto-xml”将字符串转换为 XML,它似乎创建了一个 XML 对象,但我似乎无法解析它。 I readily admit I am a newbie when it comes to dealing with XML, but I cannot seem to be able to parse the objects.我很容易承认我是处理 XML 的新手,但我似乎无法解析这些对象。 I am specifically trying to find the value of the parameter named "TO".我特别想找到名为“TO”的参数的值。 I have tried looking for "childnodes" and "innertext" but cannot enumerate the elements.我曾尝试寻找“childnodes”和“innertext”,但无法枚举元素。 I am not even sure if, after conversion, these are elements or attributes.我什至不确定转换后这些是元素还是属性。

Here is the string:这是字符串:

$String = "
<ParameterValues>
    <ParameterValue>
        <Name>TO</Name>
        <Value>Address1@domain.com;address2@domain.com</Value>
    </ParameterValue>
    <ParameterValue>
        <Name>Subject</Name>
        <Value>Emailsubject</Value>
    </ParameterValue>
    <ParameterValue>
        <Name>Comment</Name>
        <Value>ReportComment</Value>
    </ParameterValue>
    <ParameterValue>
        <Name>RenderFormat</Name>
        <Value>MHTML</Value>
    </ParameterValue>
    <ParameterValue>
        <Name>Priority</Name>
        <Value>Normal</Value>
    </ParameterValue>
    <ParameterValue>
        <Name>IncludeLink</Name>
        <Value>true</Value>
    </ParameterValue>
    <ParameterValue>
        <Name>IncludeReport</Name>
        <Value>true</Value>
    </ParameterValue>
</ParameterValues>
"

I used: $Newstring = $string |我用过: $Newstring = $string | convertto-xml转换为 xml

to get the XML object.获取 XML 对象。 How do I address the parametervalue "TO"?如何处理参数值“TO”? Dot addressing doesn't seem to work点寻址似乎不起作用

As vonPryz commented, you can use the [xml] type accellerator to read the string as XML.正如vonPryz评论的那样,您可以使用[xml]类型加速器将字符串读取为 XML。

Then it is quite easy to get the values you seek:然后很容易得到你想要的值:

[xml]$xml = $string
$to = ($xml.ParameterValues.ParameterValue | Where-Object { $_.Name -eq 'TO' }).Value -split ';'
$to

Result:结果:

Address1@domain.com
address2@domain.com

Likewise to @Theo's good answer there are a few ways to get at the data, but they are all rooted in the [XML] type accelerator.@Theo 的好答案类似,有几种方法可以获取数据,但它们都植根于 [XML] 类型加速器中。

$String = "
<ParameterValues>
    <ParameterValue>
        <Name>TO</Name>
        <Value>Address1@domain.com;address2@domain.com</Value>
    </ParameterValue>
    <ParameterValue>
        <Name>Subject</Name>
        <Value>Emailsubject</Value>
    </ParameterValue>
    <ParameterValue>
        <Name>Comment</Name>
        <Value>ReportComment</Value>
    </ParameterValue>
    <ParameterValue>
        <Name>RenderFormat</Name>
        <Value>MHTML</Value>
    </ParameterValue>
    <ParameterValue>
        <Name>Priority</Name>
        <Value>Normal</Value>
    </ParameterValue>
    <ParameterValue>
        <Name>IncludeLink</Name>
        <Value>true</Value>
    </ParameterValue>
    <ParameterValue>
        <Name>IncludeReport</Name>
        <Value>true</Value>
    </ParameterValue>
</ParameterValues>
"

$String = [XML]$String

( $String.ParameterValues.ParameterValue | 
Where-Object{ $_.Name -eq 'TO' } ).Value

A slightly shorter version using the .Where() method:使用.Where()方法的略短版本:

#...
( $String.ParameterValues.ParameterValue).Where( { $_.Name -eq 'TO' } ).Value

The particular XML structure makes this a bit confusing.特定的 XML 结构使这有点令人困惑。 Inside each ParameterValue element is essentially a Name/Value pair that in concept looks a lot like a dictionary object.在每个 ParameterValue 元素内部本质上是一个 Name/Value 对,它在概念上看起来很像一个字典对象。 So if you have to go beyond getting just the one value maybe you can actually convert to a dictionary;因此,如果您必须超越仅获取一个值,也许您实际上可以转换为字典; something like:就像是:

$Hash = @{}
$String.ParameterValues.ParameterValue |
ForEach-Object{
    $Hash.Add( $_.Name, $_.Value )
}

$Hash.To
$Hash.Subject
$Hash.Comment
# ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM