<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>youdidwhatwithtsql.com &#187; xml</title>
	<atom:link href="http://www.youdidwhatwithtsql.com/tag/xml/feed" rel="self" type="application/rss+xml" />
	<link>http://www.youdidwhatwithtsql.com</link>
	<description>making DBAs everywhere curse!</description>
	<lastBuildDate>Tue, 31 Jan 2012 12:21:23 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Converting CSV FileS to XML with Powershell</title>
		<link>http://www.youdidwhatwithtsql.com/converting-csv-files-to-xml-with-powershell/408</link>
		<comments>http://www.youdidwhatwithtsql.com/converting-csv-files-to-xml-with-powershell/408#comments</comments>
		<pubDate>Wed, 28 Oct 2009 21:34:58 +0000</pubDate>
		<dc:creator>Rhys</dc:creator>
				<category><![CDATA[Powershell]]></category>
		<category><![CDATA[csv]]></category>
		<category><![CDATA[Data]]></category>
		<category><![CDATA[Powershell Scripting]]></category>
		<category><![CDATA[xml]]></category>

		<guid isPermaLink="false">http://www.youdidwhatwithtsql.com/converting-csv-files-to-xml-with-powershell/408</guid>
		<description><![CDATA[Powershell is a pretty cool tool for many things including working with data. It’s just such a great time saver if you have to deal with multiple files or need to change them into different formats. Here’s how easy it is to turn a csv file into well-formed xml. ?View Code POWERSHELL# csv file to [...]<p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/converting-csv-files-to-xml-with-powershell/408">Converting CSV FileS to XML with Powershell</a></p>
]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> is a pretty cool tool for many things including working with data. It’s just such a great time saver if you have to deal with multiple files or need to change them into different formats. Here’s how easy it is to turn a csv file into well-formed xml.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p408code4'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4084"><td class="code" id="p408code4"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># csv file to convert</span>
<span style="color: #800080;">$csv</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;C:\Users\Rhys\Desktop\csv\file1.csv&quot;</span>;
<span style="color: #008000;"># xml file to create</span>
<span style="color: #800080;">$xml</span> <span style="color: pink;">=</span> <span style="color: #800000;">&quot;C:\Users\Rhys\Desktop\csv\file1.xml&quot;</span>;
&nbsp;
<span style="color: #008080; font-weight: bold;">Import-Csv</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800080;">$csv</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Export-Clixml</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800080;">$xml</span>;</pre></td></tr></table></div>

<p>This will produce xml looking something like this.</p>
<p>&lt;?XML:NAMESPACE PREFIX = [default] http://schemas.microsoft.com/powershell/2004/04 NS = &quot;http://schemas.microsoft.com/powershell/2004/04&quot; /&gt;&lt;?XML:NAMESPACE PREFIX = [default] http://schemas.microsoft.com/powershell/2004/04 NS = &quot;http://schemas.microsoft.com/powershell/2004/04&quot; /&gt;&lt;objs xmlns=&quot;http://schemas.microsoft.com/powershell/2004/04&quot; version=&quot;1.1.0.1&quot;&gt;<br />
  &lt;obj refid=&quot;0&quot;&gt;<br />
    &lt;tn refid=&quot;0&quot;&gt;<br />
      &lt;t&gt;System.Management.Automation.PSCustomObject&lt;/t&gt;<br />
      &lt;t&gt;System.Object&lt;/t&gt;<br />
    &lt;/tn&gt;<br />
    &lt;ms&gt;<br />
      &lt;s n=&quot;FirstName&quot;&gt;Rhys&lt;/s&gt;<br />
      &lt;s n=&quot;LastName&quot;&gt;Campbell&lt;/s&gt;<br />
      &lt;s n=&quot;Age&quot;&gt;29&lt;/s&gt;<br />
    &lt;/ms&gt;<br />
  &lt;/obj&gt;<br />
  &lt;obj refid=&quot;1&quot;&gt;<br />
    &lt;tnref refid=&quot;0&quot;&gt;&lt;/tnref&gt;<br />
    &lt;ms&gt;<br />
      &lt;s n=&quot;FirstName&quot;&gt;Joe&lt;/s&gt;<br />
      &lt;s n=&quot;LastName&quot;&gt;Bloggs&lt;/s&gt;<br />
      &lt;s n=&quot;Age&quot;&gt;40&lt;/s&gt;<br />
    &lt;/ms&gt;<br />
  &lt;/obj&gt;<br />
  &lt;obj refid=&quot;2&quot;&gt;<br />
    &lt;tnref refid=&quot;0&quot;&gt;&lt;/tnref&gt;<br />
    &lt;ms&gt;<br />
      &lt;s n=&quot;FirstName&quot;&gt;Steve&lt;/s&gt;<br />
      &lt;s n=&quot;LastName&quot;&gt;Smith&lt;/s&gt;<br />
      &lt;s n=&quot;Age&quot;&gt;35&lt;/s&gt;<br />
    &lt;/ms&gt;<br />
  &lt;/obj&gt;<br />
&lt;/objs&gt;</p>
<p>With the inclusion of the <a href="http://www.microsoft.com/technet/scriptcenter/topics/msh/cmdlets/get-childitem.mspx" target="_blank">Get-ChildItem cmdlet</a> we can merge multiple csv files into a single xml file with just a few lines of code.</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p408code5'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4085"><td class="code" id="p408code5"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Folder of csv files</span>
<span style="color: #800080;">$csvFiles</span> <span style="color: pink;">=</span> <span style="color: #008080; font-weight: bold;">Get-ChildItem</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800000;">&quot;C:\Users\Rhys\Desktop\csv\*&quot;</span> <span style="color: #008080; font-style: italic;">-Include</span> <span style="color: pink;">*</span>.csv;
&nbsp;
<span style="color: #008000;"># Process each csv file. Need to be the same structure</span>
<span style="color: #0000FF;">foreach</span><span style="color: #000000;">&#40;</span><span style="color: #800080;">$file</span> <span style="color: #0000FF;">in</span> <span style="color: #800080;">$csvFiles</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
	<span style="color: #800080;">$csvContent</span> <span style="color: pink;">+=</span> <span style="color: #008080; font-weight: bold;">Import-Csv</span> <span style="color: #008080; font-style: italic;">-Path</span> <span style="color: #800080;">$file</span>;
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #008000;"># Export the imported data as one xml file</span>
<span style="color: #800080;">$csvContent</span> <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Export-Clixml</span> <span style="color: #008080; font-style: italic;">-Path</span> C:\Users\Rhys\Desktop\csv\merged.xml;</pre></td></tr></table></div>

<p>Turning this back into a csv file is a simple <a href="http://www.microsoft.com/windowsserver2003/technologies/management/powershell/default.mspx" target="_blank">Powershell</a> one-liner!</p>

<div class="wp_codebox_msgheader"><span class="right"><sup><a href="http://www.ericbess.com/ericblog/2008/03/03/wp-codebox/#examples" target="_blank" title="WP-CodeBox HowTo?"><span style="color: #99cc00">?</span></a></sup></span><span class="left"><a href="javascript:;" onclick="javascript:showCodeTxt('p408code6'); return false;">View Code</a> POWERSHELL</span><div class="codebox_clear"></div></div><div class="wp_codebox"><table><tr id="p4086"><td class="code" id="p408code6"><pre class="powershell" style="font-family:monospace;"><span style="color: #008000;"># Turn this back into csv</span>
<span style="color: #008080; font-weight: bold;">Import-Clixml</span> <span style="color: #008080; font-style: italic;">-Path</span> C:\Users\Rhys\Desktop\csv\merged.xml <span style="color: pink;">|</span> <span style="color: #008080; font-weight: bold;">Export-Csv</span> <span style="color: #008080; font-style: italic;">-Path</span> C:\Users\Rhys\Desktop\csv\BackToCsv.csv <span style="color: #008080; font-style: italic;">-NoTypeInformation</span>;</pre></td></tr></table></div>

<p>Check out my other data related Powershell posts; <a href="http://www.youdidwhatwithtsql.com/splitting-csv-files-with-powershell/374" target="_blank">splitting csv files with Powershell</a>, <a href="http://www.youdidwhatwithtsql.com/merging-csv-files-with-powershell/330" target="_blank">merging csv files with Powershell</a> and <a href="http://www.youdidwhatwithtsql.com/trimming-whitespace-with-powershell/388" target="_blank">trimming whitespace with Powershell</a>.</p>
<p><map name='google_ad_map_408_a45beff5d2e172f6'>
<area shape='rect' href='http://imageads.googleadservices.com/pagead/imgclick/408?pos=0' coords='1,2,367,28' />
<area shape='rect' href='http://services.google.com/feedback/abg' coords='384,10,453,23'/></map>
<img usemap='#google_ad_map_408_a45beff5d2e172f6' border='0' src='http://imageads.googleadservices.com/pagead/ads?format=468x30_aff_img&amp;client=&amp;channel=&amp;output=png&amp;cuid=408&amp;url=http%3A%2F%2Fwww.youdidwhatwithtsql.com%2Fconverting-csv-files-to-xml-with-powershell%2F408' title="Converting CSV FileS to XML with Powershell" alt=" Converting CSV FileS to XML with Powershell" /></p><p>Post from: <a href="http://www.youdidwhatwithtsql.com">youdidwhatwithtsql.com</a><br/><br/><a href="http://www.youdidwhatwithtsql.com/converting-csv-files-to-xml-with-powershell/408">Converting CSV FileS to XML with Powershell</a></p>
<div class="none"><div class="g-plusone" data-href="http://www.youdidwhatwithtsql.com/converting-csv-files-to-xml-with-powershell/408" size="standard" count="true"></div></div>]]></content:encoded>
			<wfw:commentRss>http://www.youdidwhatwithtsql.com/converting-csv-files-to-xml-with-powershell/408/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

