Insert(append) a first line in xml file (1 Viewer)

MBMSOFT

Registered User.
Local time
Today, 10:51
Joined
Jan 29, 2010
Messages
90
I need to insert(append) first and last line in xml file...
So I have xml file and I need to insert one line to the top and at the of the text... Xml file contains Cyrillic characters and method to recreate xml file is going wrong with characters conversions... The last line a can append easy but the first line is a problem...
Any ideas about?
 

MarkK

bit cruncher
Local time
Today, 02:51
Joined
Mar 17, 2004
Messages
8,186
Here are a set of instructions that should work
1) open the file and read it's contents into a variable
Code:
var = TextStream.ReadAll
2) write your first and last lines into the variable
Code:
var = firstline & vbcrlf & var & lastline
3) delete the original file
4) write the contents of the variable to a new file with the same name
Hope this helps,
 

MBMSOFT

Registered User.
Local time
Today, 10:51
Joined
Jan 29, 2010
Messages
90
The problem is that if i get text from old file in vbcrlf beacuse of cyrillic charcters vb make some conversion of the characters and then file is not usefull so I can't delete old file... I need only to append line... the last line is ok i just append... but i can't insert first line in the text..
 

MarkK

bit cruncher
Local time
Today, 02:51
Joined
Mar 17, 2004
Messages
8,186
Yeah, well, I don't think you can insert text at the beginning of a text file, and I don't see how any text issues that might arise writing a new file would not also arise doing an insert--assuming it was possible.

Also, I don't understand what this means, and maybe it would make a difference . . .
get text from old file in vbcrlf beacuse of

Cheers,
 

MBMSOFT

Registered User.
Local time
Today, 10:51
Joined
Jan 29, 2010
Messages
90
Problem is about encoding of the files somehow...
The new file = var = firstline & vbcrlf(text from start file) & var & lastline
has wrong encoding because start file has Cyrillic characters inside... so when i try to import xml in access tables...error is appearing like iligal characters...
 

gemma-the-husky

Super Moderator
Staff member
Local time
Today, 10:51
Joined
Sep 12, 2006
Messages
15,660
how does the file get prepared at present?
Is it reported as a valid xml file?
 

MBMSOFT

Registered User.
Local time
Today, 10:51
Joined
Jan 29, 2010
Messages
90
I create file with for xml auto :
exec master..xp_cmdshell 'bcp "select * from mytable" as ROW for xml auto , elements, queryout "c:\file.xml" -T -C 1251 -w -r -t'
But I don't why root element does not want to be created... This does not work:
exec master..xp_cmdshell 'bcp "select * from mytable" as ROW for xml auto , root " rows" , elements, queryout "c:\file.xml" -T -C 1251 -w -r -t'
So I must append root element in the file....
But if I go to do that with
var = firstline & vbcrlf & var & lastline
VBA editor convert the text from first xml files(cyrillic characters) and new xml file does not work...
If I add root element by writng to the file than is ok but if go through vba editor I get mess...
 

vbaInet

AWF VIP
Local time
Today, 10:51
Joined
Jan 22, 2010
Messages
26,374
I can't advise you on the bcp batch code at the moment but the following code re your main post should preserve your text:
Code:
    Dim objFso      As Scripting.FileSystemObject
    Dim objStream   As Scripting.TextStream
    Dim strContents As String
    
    ' Make a copy of the file
    FileCopy [COLOR="blue"]FILE_PATH[/COLOR], [COLOR="Blue"]FILE_DESTINATION[/COLOR]
    
    ' Grab contents
    Set objStream = objFso.OpenTextFile([COLOR="blue"]FILE_PATH[/COLOR], ForReading, , TristateTrue)
    With objStream
        strContents = .ReadAll
        .Close
    End With
    
    ' Write new lines
    Set objStream = objFso.OpenTextFile([COLOR="blue"]FILE_PATH[/COLOR], ForWriting, , TristateTrue)
    With objStream
        .WriteLine "First Line"
        .WriteLine strContents
        .Write "Last Line"
        .Close
    End With

    Set objStream = Nothing
    Set objFso = Nothing
There's another way if that doesn't work.
 

vbaInet

AWF VIP
Local time
Today, 10:51
Joined
Jan 22, 2010
Messages
26,374
Should have mentioned that it's similar to what MarkK advised. The main points highlighted are the TriStateTrue property and the fact that you're utilising the same file when writing back.
 

MBMSOFT

Registered User.
Local time
Today, 10:51
Joined
Jan 29, 2010
Messages
90
Thanks a lot vbaInet.... I spend a whole week fighting with this... Finally I can import Cyrillic text into my tables with xml...
BTW... I just little modify your code so I hope this post will be hopefull for another users... By the way there are no enough answers about this around net...
NOTE for another users: Format text : tristatetrue to unicode; tristatefalse to ascii; tristatedefault to systemdefault...

Dim objFso As Scripting.FileSystemObject
Dim objStream As Scripting.TextStream
Dim strContents As String

File = "C:\A\myfile.xml"

'Grab contents
Set objFso = CreateObject("Scripting.filesyStemObject")
Set objStream = objFso_OpenTextFile(File, ForReading, , TristateTrue)
With objStream
strContents = .ReadAll
.Close
End With

' Write new lines
Set objStream = objFso_OpenTextFile(File, ForWriting, , TristateTrue)
With objStream
.WriteLine "<ROOT>"
.WriteLine strContents
.Write "</ROOT>"
.Close
End With

Set objStream = Nothing
Set objFso = Nothing
End Sub
 

vbaInet

AWF VIP
Local time
Today, 10:51
Joined
Jan 22, 2010
Messages
26,374
Good job!
NOTE for another users: Format text : tristatetrue to unicode; tristatefalse to ascii; tristatedefault to systemdefault...
There's one more, TristateMixed which attempts to open it in Ascii or Unicode.

By the way, I would still include the code to copy the file just in case, and then delete the copy after a successful write process.
 

Users who are viewing this thread

Top Bottom