I want to read the text of an XML node from my config file. For this I have used Xpath to read the values. One of the config values is having the space between it's conetents, so when I am reading that it's just returning the value till the white space. My config filename and content as below config.xml
<?xml version="1.0" encoding="UTF-8"?>
<OpsConfig>
<Tenant>mytenant_az1</Tenant>
<Client>XYZ</Client>
<ReviewID>a123456</ReviewID>
<ReferenceID>ARTL_Is Correction_Rewind_Report</ReferenceID>
<OpsConfig>
I am successfully able to read the tag values except for the tag ReferenceID . Below is the code:
$configFile = "config.xml"
[xml]$confXml = Get-Content $configFile
$refIds = $confXml | Select-XML -XPath "//OpsConfig/ReferenceID"
Write-Host $refIds
The variable $refIds just returns "ARTL_Is". It's skipping the content after the space. I tried by appending the hexa value of space but still didn't return the whole value "ARTL_Is Correction_Rewind_Report".
The issue you are facing is because of the closure tag which seems missing. I don't know that is a typo or not. But When I kept it there and trying to read the content as XML parsing , I am able to retrieve it.
Could you try this:
<OpsConfig>
<Tenant>mytenant_az1</Tenant>
<Client>XYZ</Client>
<ReviewID>a123456</ReviewID>
<ReferenceID>ARTL_Is Correction_Rewind_Report</ReferenceID>
</OpsConfig>
I am in PS version 4 just FYI. Please check the PS version also
Since you're already using Get-Content, you can just call on the variable to get its node.
[Xml]$ConfXml = Get-Content ".\config.xml"
$RefIds = $ConfXml.OpsConfig.ReferenceID
Write "$RefIds"
If your concern is multiple Reference IDs, you can parse them with a ForEach
ForEach ($i in $ConfXml.OpsConfig.ReferenceID) { Do stuff with $i }
Alternatively,
ForEach ($i in $ConfXml.OpsConfig) { Do stuff with $i.ReferenceID }
With this method, the xml attribute in the variable is just a string and shouldn't care about spaces.
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.