• 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.

<FlightInfo>
    <FlightKey>
        <Alc>SCX</Alc>
        <Number>0117</Number>
        <STD>2014-06-30T11:00:00Z</STD>
        <OriginIcao>KMSP</OriginIcao>
    </FlightKey>
    <Compute Timestamp="2014-06-30 09:09:00">
        <Fuels Unit="LBS">
            <Pre>27151</Pre>
            <Release>10651</Release>
            <Tankerage>16500</Tankerage>
            <Burn>3830</Burn>
            <Arrival>22996</Arrival>
            <Costs>
                <Origin Currency="USD" Quantity="USG">3.11</Origin>
                <Destination Currency="USD" Quantity="USG">4.00</Destination>
                <Savings Currency="USD">2098.98</Savings>
            </Costs>
        </Fuels>
        <Dispatcher>JOHNNY ROCKET</Dispatcher>
    </Compute>
</FlightInfo>

Open in new window

0
Jim Horn
Asked:
Jim Horn
  • 2
3 Solutions
 
Ron MalmsteadInformation Services ManagerCommented:
Try
            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
            MsgBox(ex.Message)
        End Try
0
 
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.

E.g.
<?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:copy>
      <xsl:apply-templates select="*|@*|text()|processing-instruction()|comment()"/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Open in new window

0
 
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...
0
 
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
Jim
0
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