I am running a SQL query and one of the fields returned contains a string that is formatted as 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. 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. I am specifically trying to find the value of the parameter named "TO". I have tried looking for "childnodes" and "innertext" but cannot enumerate the elements. 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 | convertto-xml
to get the XML object. How do I address the parametervalue "TO"? Dot addressing doesn't seem to work
As vonPryz commented, you can use the [xml]
type accellerator to read the string as 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.
$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:
#...
( $String.ParameterValues.ParameterValue).Where( { $_.Name -eq 'TO' } ).Value
The particular XML structure makes this a bit confusing. Inside each ParameterValue element is essentially a Name/Value pair that in concept looks a lot like a dictionary object. 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
# ...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.