I have this XML style file.
<?xml version="1.0" encoding="UTF-8" ?>
<searchresults timestamp='ttt' www='www' qqq='qqq' url='url'>
<obj id='00001' type='Random' name1='Mow' class='Data' >
<tags>
<tag key="Car" value="4x4" />
<tag key="City" value="Big" />
<tag key="Dog" value="Loud" />
</tags>
<details>
<name desc="fulldesc">Full description</name>
</details>
<i1>Empty</i1>
<i2>False</i2>
<i3>True</i3>
</obj>
<obj id='00002' type='Random' name1='AGP' class='BigData' >
<tags>
<tag key="Car" value="Broken" />
<tag key="City" value="Fresh" />
<tag key="Dog" value="Long" />
</tags>
<details>
<name desc="fulldesc">Good desc</name>
</details>
<i1>True</i1>
<i2></i2>
<i3>False</i3>
</obj>
</searchresults>
I need export all content to CSV. in particular I need data from tag and details containers. In this child nodes information has such scheme - key and value. I need make (key must be - column name) (value must be - key column value). (fulldesc = column name, "Full description" - content for fulldesc column) Like this
id type name class Car City Dog fulldesc i1 i2 i3 -- ---- ---- ----- ---- ---- ---- ------- -- -- -- id1 type1 name1 class1 4x4 Big Loud Full description Information1 Information2 Information3 id2 type2 name2 class2 4x4 Big Loud Full description Information1 Information2 Information3
I use this code for export - it work pretty, but I can't take some content from XML.
[xml]$inputFile = Get-Content ".\xmlFile.xml"
$inputFile.searchresults.ChildNodes |
Export-Csv ".\xmlFile.csv" -NoTypeInformation -Delimiter:";" -Encoding:UTF8
Import-Csv -Delimiter ';' -Encoding:UTF8 -Path ".\xmlFile.csv" | Format-Table
Result:
id type name class tags details i1 i2 i3 -- ---- ---- ----- ---- ------- -- -- -- id1 type1 name1 class1 System.Xml.XmlElement System.Xml.XmlElement Information1 Information2 Information3 id2 type2 name2 class2 System.Xml.XmlElement System.Xml.XmlElement Information1 Information2 Information3
In tags and details columns no content.
when I use this code:
[xml]$xml = Get-Content .\XML\12.xml
$xml.SelectNodes("//*")
I receive all information from XML:
timestamp : ttt www : www qqq : qqq url : url obj : {obj, obj} id : 00001 type : Random name1 : Mow class : Data tags : tags details : details i1 : Empty i2 : False i3 : True tag : {tag, tag, tag} key : Car value : 4x4 key : City value : Big key : Dog value : Loud name : name desc : fulldesc #text : Full description #text : Empty #text : False #text : True id : 00002 type : Random name1 : AGP class : BigData tags : tags details : details i1 : True i2 : i3 : False tag : {tag, tag, tag} key : Car value : Broken key : City value : Fresh key : Dog value : Long name : name desc : fulldesc #text : Good desc #text : True Name : i2 LocalName : i2 NamespaceURI : Prefix : NodeType : Element ParentNode : obj OwnerDocument : #document IsEmpty : False Attributes : {} HasAttributes : False SchemaInfo : System.Xml.XmlName InnerXml : InnerText : NextSibling : i3 PreviousSibling : i1 Value : ChildNodes : {} FirstChild : LastChild : HasChildNodes : False IsReadOnly : False OuterXml : <i2></i2> BaseURI : PreviousText : #text : False