locked
VB code to convert xml file to .txt

    Question

  • Hi,

    I have a xml file that I generated with a SQL query.

    I need the info from that xml file converted to a .txt file. I can not export the sql information to plain text, because I need to modify the info into a certain way as well. The first thing I need to be able to do is to convert xml to .text and then I will get to converting in some format. Can someone please help me or point me to documentation or books to read. I really need to do this and would greatly appretiate the help.

     

    Thanks!

    Saturday, February 10, 2007 5:41 PM

Answers

  • Hi,

    Could you not ouput a SQL query as a CSV file instead?

    StreamReader As StreamWriter in one example.

    You will have to put your values into a String and get the string length to pad with the "0" character as per your 2nd post.

     

    Regards,

    S_DS

    -----------------------------------------------------------------------------------------------------------------------------------

    Use:

    Dim num, numLength As Integer

    Dim myString As String = num.ToString

    numLength=myString.Length 'etc

     

    ' StreamReader & StreamWriter example below.>>>>>>>>>>>>>>>>>>>>>>>>>                                         

    Imports System
    Imports System.IO
    Imports System.Text

    Public Class Test

        Public Shared Sub Main()
            Dim path As String = "c:\temp\MyTest.txt"

            Try
                If File.Exists(path) Then
                    File.Delete(path)
                End If

                Dim sw As StreamWriter = New StreamWriter(path)
                sw.WriteLine("This")
                sw.WriteLine("is some text")
                sw.WriteLine("to test")
                sw.WriteLine("Reading")
                sw.Close()

                Dim sr As StreamReader = New StreamReader(path)

                Do While sr.Peek() >= 0
                    Console.WriteLine(sr.ReadLine())
                Loop
                sr.Close()
            Catch e As Exception
                Console.WriteLine("The process failed: {0}", e.ToString())
            End Try
        End Sub
    End Class

     

    Sunday, February 11, 2007 3:32 AM

All replies

  • Hi,

    An XML file is a text file already.

    Try loading it into NotePad or Word.

    If you want all the "/" and ">" and "<" out of the file and any other characters, you decide which.

    You could do it with a VB.Net program using StreamReader and StreamWriter.

     

    Regards,

    S_DS

     

     

     

    Saturday, February 10, 2007 8:43 PM
  • That was very helpful! For the first time I am finding documents on this issue and I have been sitting with this for a while.
     
    The user still does not make anything out of the file when they open it in Notepad and that is why I need to format the file for them.
     
    I was told to output the query in XML, because it is easier to change the format of what I need in a Text file.

    I have one element that looks like this :
    <UnModifiedAmount>975.47</UnModifiedAmount>
    <UnModifiedAmount>5.47</UnModifiedAmount>

    In the text document I need it to look like the following:
    0000097547
    0000000547

    Can you help me to do this with Streamwriter? or point me to where I can go and look.

    Thanks
    Sunday, February 11, 2007 1:47 AM
  •  

    Streamreader.>>

    http://search.microsoft.com/results.aspx?q=streamreader&mkt=en-US&FORM=SSME

    StreamWriter>>

    http://search.microsoft.com/results.aspx?mkt=en-US&setlang=en-US&q=streamwriter

    Do further searches in the main search box on http://www.microsoft.com

     or in the msdn library >>

     
    This library area is bettter as it has a search box.>>
     

    or in the forums from>>

    http://forums.microsoft.com/MSDN/default.aspx?ForumGroupID=10&SiteID=1 or

     

    http://forums.microsoft.com/MSDN/default.aspx?SiteID=1

    There are two search boxes at the top of all these FORUM pages.

    1st one searches Microsoft.com

    2nd one searches the forum area that you are currently in only i believe.

    So use it from the last link above.

     

     

    Regards,

    S_DS

     

     

    Sunday, February 11, 2007 3:08 AM
  • Hi,

    Could you not ouput a SQL query as a CSV file instead?

    StreamReader As StreamWriter in one example.

    You will have to put your values into a String and get the string length to pad with the "0" character as per your 2nd post.

     

    Regards,

    S_DS

    -----------------------------------------------------------------------------------------------------------------------------------

    Use:

    Dim num, numLength As Integer

    Dim myString As String = num.ToString

    numLength=myString.Length 'etc

     

    ' StreamReader & StreamWriter example below.>>>>>>>>>>>>>>>>>>>>>>>>>                                         

    Imports System
    Imports System.IO
    Imports System.Text

    Public Class Test

        Public Shared Sub Main()
            Dim path As String = "c:\temp\MyTest.txt"

            Try
                If File.Exists(path) Then
                    File.Delete(path)
                End If

                Dim sw As StreamWriter = New StreamWriter(path)
                sw.WriteLine("This")
                sw.WriteLine("is some text")
                sw.WriteLine("to test")
                sw.WriteLine("Reading")
                sw.Close()

                Dim sr As StreamReader = New StreamReader(path)

                Do While sr.Peek() >= 0
                    Console.WriteLine(sr.ReadLine())
                Loop
                sr.Close()
            Catch e As Exception
                Console.WriteLine("The process failed: {0}", e.ToString())
            End Try
        End Sub
    End Class

     

    Sunday, February 11, 2007 3:32 AM
  • S_DS,

    This was gret! Thank you so much! I can now start looking at the pages and teach myself what to do.

    I might post again if I get stuck.

    I really appretiate the help S_DS

     

    Thanks

    Sunday, February 11, 2007 4:34 AM
  •  Lawrence 007 wrote:

    S_DS,

    This was gret! Thank you so much! I can now start looking at the pages and teach myself what to do.

    I might post again if I get stuck.

    I really appretiate the help S_DS

     

    Thanks

    Hi,

    Your welcome, thanks for a reply. :-)   

     

    Regards,

    S_DS

     

     

    Sunday, February 11, 2007 11:19 AM
  • XML files are plain text files with special tags for each row, and each field. Its almost equivalent to a database but in text format.

    There are ready made functions to read from xml files, if you are interested. But if you only want the data from the xml files, i guess you can use Regular Expressions tools available, to rip parts of the data from that file. Or instead use built in functions. for reading xml files.
    Sunday, February 11, 2007 11:50 AM