I need to split a longer string (whole XML file) by </event>
to have each event in one element of the created list.
If I do it in a naive way:
$evList = $txtAll.Split("</event>")
I get rubbish. So I ended up doing it this way:
$evList = $txtAll.replace("</event>","|").Split("|")
replacing </event>
by |
and then splitting at |
.
Once in a while this (or other) strange character appears and again I have a mess.
Is there a way to split the long string directly by </event>
? How?
When in doubt, read the documentation. If you want to split a string at substrings using the Split()
method you need to make the separator an array of strings:
$evList = $txtAll.Split([string[]]"</event>", [StringSplitOptions]::None)
otherwise the separator argument will be interpreted as an array of characters, so you'd be splitting the string at any of the characters in your given separator string.
Or you could use the -split
operator, which will allow you to split a string at a regular expression:
$evList = $txtAll -split "</event>"
Make sure the separator string doesn't contain special characters, e.g. by escaping it like this:
$evList = $txtAll -split [regex]::Escape("</event>")
With that said, if you need to extract data from an XML file you'd be far better off using an actual XML parser, e.g. like this:
[xml]$xml = Get-Content 'C:\path\to\your.xml'
$xml.SelectNodes('//event') | Select-Object -Expand InnerText
or like this (using the Select-Xml
cmdlet):
Select-Xml -Path 'C:\path\to\your.xml' -XPath '//event' |
Select-Object -Expand Node |
Select-Object -Expand InnerText
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With