Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

XML To Text in VB .NET

Does anyone out there have any ideas on how to convert XML to a text file?
Thanks in advance!!
LJ
[110 byte] By [LJ] at [2007-11-9 15:27:10]
# 1 Re: XML To Text in VB .NET
Here is my code in VB .NET:

'When the "Save" button is clicked an XML document is created
'from the values on the Form
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As _
System.EventArgs) Handles btnSave.Click
'create a new ticket object
Dim ticket As New Ticket()
'copy the values from the form into the ticket
PopulateTicketFromForm(ticket)
'save the ticket
Dim filename As String = DataFilename
ticket.Save(filename)
'tell the user
MsgBox("The ticket was saved to " & filename)
End Sub

'DataFilename - where the Data is stored
Public ReadOnly Property DataFilename() As String
Get
'get the working folder
Dim folder As String
folder = Environment.CurrentDirectory
'return the folder with the name "MSTicketDetail.xml"
Return folder & "\MSTicketDetail.xml"
End Get
End Property

'PopulateTicketFromForm - populates the Ticket from the form fields
Public Sub PopulateTicketFromForm(ByVal ticket As Ticket)
'copy the values
ticket.CompanyNumber = cboCoNum.Text
ticket.Month = cboMonth.Text
ticket.TransNum = txtTransNum.Text
End Sub

Here is the XML document:

<?xml version="1.0"?>
<Ticket xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<CuompanyNumber>50</CuompanyNumber>
<Month>Octo</Month>
<TransNum>123456</TransNum>
</Ticket>

In case you haven't realized, I am new at XML & .NET

Thanks again!


"Russell Jones" <arj1@nospam.northstate.net> wrote:
>You can use XSLT or you can process XML with an XmlTextReader or an
>XmlDocument object to output text. If you post an example of what you're
>trying to do, someone here will help.
>
>Russell Jones
>Executive Editor,
>dev-archive.com
>"LJ" <ljwinters@hotmail.com> wrote in message
>news:3dc13a4a@tnews.web.dev-archive.com...
>>
>> Does anyone out there have any ideas on how to convert XML to a text file?
>>
>> Thanks in advance!!
>> LJ
>
>
LJ at 2007-11-11 23:29:47 >
# 2 Re: XML To Text in VB .NET
You can use XSLT or you can process XML with an XmlTextReader or an
XmlDocument object to output text. If you post an example of what you're
trying to do, someone here will help.

Russell Jones
Executive Editor,
dev-archive.com
"LJ" <ljwinters@hotmail.com> wrote in message
news:3dc13a4a@tnews.web.dev-archive.com...
>
> Does anyone out there have any ideas on how to convert XML to a text file?
>
> Thanks in advance!!
> LJ
Russell Jones at 2007-11-11 23:30:49 >
# 3 Re: XML To Text in VB .NET
I assume the ticket.Save method saves an XML file such as the one you
included. That;s *already* a text file. Does the ticket class have a
corresponding Load or Read method? What exactly is it that you want to do
with the saved ticket XML file?

"LJ" <ljwinters@hotmail.com> wrote in message
news:3dc14971$1@tnews.web.dev-archive.com...
>
> Here is my code in VB .NET:
>
> 'When the "Save" button is clicked an XML document is created
> 'from the values on the Form
> Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As _
> System.EventArgs) Handles btnSave.Click
> 'create a new ticket object
> Dim ticket As New Ticket()
> 'copy the values from the form into the ticket
> PopulateTicketFromForm(ticket)
> 'save the ticket
> Dim filename As String = DataFilename
> ticket.Save(filename)
> 'tell the user
> MsgBox("The ticket was saved to " & filename)
> End Sub
>
> 'DataFilename - where the Data is stored
> Public ReadOnly Property DataFilename() As String
> Get
> 'get the working folder
> Dim folder As String
> folder = Environment.CurrentDirectory
> 'return the folder with the name "MSTicketDetail.xml"
> Return folder & "\MSTicketDetail.xml"
> End Get
> End Property
>
> 'PopulateTicketFromForm - populates the Ticket from the form fields
> Public Sub PopulateTicketFromForm(ByVal ticket As Ticket)
> 'copy the values
> ticket.CompanyNumber = cboCoNum.Text
> ticket.Month = cboMonth.Text
> ticket.TransNum = txtTransNum.Text
> End Sub
>
> Here is the XML document:
>
> <?xml version="1.0"?>
> <Ticket xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
> <CuompanyNumber>50</CuompanyNumber>
> <Month>Octo</Month>
> <TransNum>123456</TransNum>
> </Ticket>
>
> In case you haven't realized, I am new at XML & .NET
>
> Thanks again!
>
>
>
>
>
>
> "Russell Jones" <arj1@nospam.northstate.net> wrote:
> >You can use XSLT or you can process XML with an XmlTextReader or an
> >XmlDocument object to output text. If you post an example of what you're
> >trying to do, someone here will help.
> >
> >Russell Jones
> >Executive Editor,
> >dev-archive.com
> >"LJ" <ljwinters@hotmail.com> wrote in message
> >news:3dc13a4a@tnews.web.dev-archive.com...
> >>
> >> Does anyone out there have any ideas on how to convert XML to a text
file?
> >>
> >> Thanks in advance!!
> >> LJ
> >
> >
>
Russell Jones at 2007-11-11 23:31:54 >
# 4 Re: XML To Text in VB .NET
The most efficient way to handle this is to read through the XML with the
XmlTextReader class and then as you find the data you want write it to a
file with a StreamWriter or store it in something like a StringBuilder (which
could then be written to a file). I don't have any examples going from XML
to text unfortunately but do have a lot of other examples at the following
URL that may help you out:

http://www.xmlforasp.net/content.aspx?content=search&searchText=xmltextreader

You could also write an XSLT stylesheet to handle the transformation from
XML to text file but that may or may not be a good solution depending upon
the size of your XML document.

HTH,
Dan Wahlin

http://www.XMLforASP.NET
Microsoft MVP - ASP.NET
XML for ASP.NET Developers in bookstores everywhere.

"LJ" <ljwinters@hotmail.com> wrote:
>
>Does anyone out there have any ideas on how to convert XML to a text file?
>
>Thanks in advance!!
>LJ
Dan Wahlin at 2007-11-11 23:32:52 >