I have an XML file looks like this :
<?xml version="1.0" encoding="UTF-8"?>
<INITIAL>
<FB1>a</FB1>
<FB2>b</FB2>
<FB3>c</FB3>
<FB4>d</FB4>
<FB5>e</FB5>
<FB6>f</FB6>
<FB7>g</FB7>
<FB8>h</FB8>
<FB9>i</FB9>
</INITIAL>
I just want to read element a to iand convert it to string, be like this :
abcdefghi
You first create an System.Xml.XmlDocument out of it. Here is an example:
$theXml = [xml](gc $xmlPath)
$theXml.GetType().fullname
Once you have it, you can play-around the way you want! - In your example, you need concatenation of values of child elements. You would find lot of example online how to concatenate string values from an Array - example
$theXml.INITIAL.GetEnumerator() # this gives you enumeration of values in children to the 'INITIAL' element.
I Hope this helps.
Edit: And yeah, your xml was broken - I assume it added closing tags like </FB1>
for all.
You can use an XPath expression to get the nodes you want and then use the -join
operator to join the strings together.
The XPath expression /INITIAL/*
means every element directly inside the INITIAL
element at top level. Taking care to select the data you actually want will help you if someone later decides to extend the data format somehow.
PS> $Data = New-Object Xml
PS> $Data.Load((Resolve-Path file.xml))
PS> $Data.SelectNodes("/INITIAL/*")."#text" -join ""
frfsftfufvfwfhgKfg
Tested with Windows PowerShell 5.1 on Windows 10.
Assuming this XML:
<INITIAL>
<FB1>fr</FB1>
<FB2>fs</FB2>
<FB3>...</FB3>
</INITIAL>
fetching the InnerText
of the container element will be enough:
Write-Host $xml.DocumentElement.InnerText
This would print frfs...
.
If in your actual XML the container isn't the top-most element, substitute DocumentElement
with .SelectSingleNode('some/xpath')
.
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.