BigBlack
BigBlack

Reputation: 163

Batch file to merge xml files from SQL Server 2005 bcp export

I have several xml files in a folder generated from a bcp export in SQL Server 2005. I discovered that xml export with bcp doesn't generate internet explorer compatible xml. But I've discovered also that if I create a new empty xml file and copy those xml files content in this new file, the file is compatible with internet explorer.

My need is to create a batch file to make this copy for each file on this folder. I try with the cmd copy file.xml file1.xml but the file is still not compatible. There is another way to do that creating an internet explorer compatible xml?

Upvotes: 1

Views: 145

Answers (1)

rojo
rojo

Reputation: 24466

My vote is to use an XML parser, rather than trying to merge the XML data as flat text. Unfortunately, the Batch language doesn't offer much in the way of XML parsing. PowerShell does, though. Here's an example PowerShell script that will read all *.xml files and clone their contents into an XML object, saving it at the end to a file named joined.xml:

# File to save:
$outfile = "joined.xml"

# create an XML object with a single empty container node
$x = [xml]"<root></root>"

# Delete previous export if exists to avoid cumulative runs
if (test-path $outfile) { del $outfile }

# For each XML file...
gci *.xml | %{

    # Read the contents and cast as an XML object.  Then for each root node...
    ([xml](gc $_)).selectNodes("/*") | %{

        # Clone the node and its descendants into our XML object's container node.
        [void]$x.selectSingleNode("/root").AppendChild($x.ImportNode($_, $true))
    }
}

# Finally, save the resulting XML data to a file.
$x.Save($outfile)

Upvotes: 1

Related Questions