• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 312
  • Last Modified:

SSIS 2012 Script Task: How to add text to a file before working with it (prefer VB)

Hi all

Question:  How in SSIS 2012 can I take a file such as the below code, insert the string<?xml version="1.0"?> in the beginning, then close it?  

Connections are fine.  Would prefer a VB script solution, but C# is fine.

    <Compute Timestamp="2014-06-30 09:09:00">
        <Fuels Unit="LBS">
                <Origin Currency="USD" Quantity="USG">3.11</Origin>
                <Destination Currency="USD" Quantity="USG">4.00</Destination>
                <Savings Currency="USD">2098.98</Savings>
        <Dispatcher>JOHNNY ROCKET</Dispatcher>

Open in new window

Jim Horn
Jim Horn
  • 2
3 Solutions
Ron MalmsteadInformation Services ManagerCommented:
            Dim filecontents As String = "<?xml version=" & """" & "1.0" & """" & "?>" & vbCrLf
            filecontents = filecontents & FileIO.FileSystem.ReadAllText("c:\test.txt").ToString

            FileIO.FileSystem.WriteAllText("c:\test.txt", filecontents, False)
        Catch ex As Exception
        End Try
ste5anSenior DeveloperCommented:
Why? The XML declaration is optional.

When it's really necessary I would use a XSLT to transform it, e.g. with MSXML on the command line or a XML Task.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" omit-xml-declaration="no" />
  <xsl:template match="@*|*|processing-instruction()|comment()">
      <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()"/>

Open in new window

Jim HornMicrosoft SQL Server Developer, Architect, and AuthorAuthor Commented:
I've been reading that it's optional, but when I use it in a Data Source Task I get a 'Data at the root level is invalid' error message, and the quick workaround I discovered is to manually open it up and paste <?xml version="1.0"?> at the beginning of the file.  That question is here.

Unless there's a property somewhere that I'm not seeing...
Jim HornMicrosoft SQL Server Developer, Architect, and AuthorAuthor Commented:
Hmm.  Now it's working, and I don't know why, as the only changes where moving the root folder, and changing the XSD location to be parameter-driven instead of hard-coded.

Thanks for the help
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now