473,385 Members | 1,347 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Write XML to a Text File

Hi,

I have a XML file that I need to write to a text file. I have the following code that I got from this C# article http://support.microsoft.com/default.aspx/kb/317662:

Expand|Select|Wrap|Line Numbers
  1. Try
  2.             Dim doc As Xml.XmlDocument = New Xml.XmlDocument
  3.             doc.Load("c:\XSL\sales.xml")
  4.             doc.Save("c:\XSL\Finally!!!.txt")
  5.             Dim memStream As IO.MemoryStream = New IO.MemoryStream
  6.             Dim AE As System.Text.ASCIIEncoding = New System.Text.ASCIIEncoding
  7.             Dim xmlStr As String = doc.DocumentElement.OuterXml
  8.             memStream.Write(AE.GetBytes(xmlStr), 0, xmlStr.Length)
  9.             memStream.Close()
  10.             doc.PreserveWhitespace = True
  11.             Dim wrtr As Xml.XmlTextWriter = New Xml.XmlTextWriter("c:\XSL\Finally!!!.txt", System.Text.Encoding.ASCII)
  12.             wrtr.WriteRaw(xmlStr)
  13.             wrtr.Close()
  14.         Catch xmlEx As Xml.XmlException
  15.             Console.WriteLine("{0}", xmlEx.Message)
  16.         Catch ex As Exception
  17.             Console.WriteLine("{0}", ex.Message)
  18.         Finally
  19.         End Try
It is working for me to write the XML file. I have a XSL file that I created to format the information for me in understandable format, but I am struggling to get that into my code. I would really appreciate the help.

Lawrence
Mar 21 '07 #1
2 1466
For all that is interested.

This is what I did to resolve my issue

Expand|Select|Wrap|Line Numbers
  1. Imports System
  2. Imports System.Collections.Generic
  3. Imports System.ComponentModel
  4. Imports System.Data
  5. Imports System.Drawing
  6. Imports System.Text
  7. Imports System.Windows.Forms
  8. Imports System.Xml
  9. Imports System.Xml.Xsl
  10. Imports System.Xml.XPath
  11. Imports System.IO
  12. Public Class XMLWriter
  13.  
  14.  
  15.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  16.  
  17.         Dim ds As DataSet
  18.         Dim xmlDoc As XmlDataDocument
  19.         Dim xslTran As XslCompiledTransform
  20.         Dim root As XmlElement
  21.         Dim nav As XPathNavigator
  22.         Dim writer As XmlTextWriter
  23.  
  24.         Try
  25.  
  26.             'Create the DataSet from the XML file
  27.             ds = New DataSet()
  28.             ds.ReadXml("C:\XSL\Sales.xml")
  29.  
  30.             'Create the XML from the DataSet
  31.             xmlDoc = New XmlDataDocument(ds)
  32.  
  33.             'Load the XSLT for Transformation
  34.             xslTran = New XslCompiledTransform()
  35.             xslTran.Load("C:\XSL\SalesXSL.xsl")
  36.  
  37.             'Determine the Root object in the XML
  38.             root = xmlDoc.DocumentElement
  39.  
  40.             'Create the XPath Navigator to navigate throuth the XML
  41.             nav = root.CreateNavigator()
  42.  
  43.             'First delete the RTF, if already exist
  44.             If File.Exists("C:\XSL\Finally!!!.txt") Then
  45.                 File.Delete("C:\XSL\Finally!!!.txt")
  46.             End If
  47.  
  48.             'Create the RTF by Transforming the XML and XSLT
  49.             writer = New XmlTextWriter("C:\XSL\Finally!!!.txt", System.Text.Encoding.ASCII)
  50.             xslTran.Transform(nav, writer)
  51.  
  52.             'Close the Writer after Transformation
  53.             writer.Close()
  54.  
  55.             'Release all objects
  56.             writer = Nothing
  57.             nav = Nothing
  58.             root = Nothing
  59.             xmlDoc = Nothing
  60.             ds = Nothing
  61.  
  62.             MessageBox.Show("Document created successfully.....")
  63.  
  64.         Catch ex As Exception
  65.             writer = Nothing
  66.             nav = Nothing
  67.             root = Nothing
  68.             xmlDoc = Nothing
  69.             ds = Nothing
  70.  
  71.             MessageBox.Show(ex.StackTrace)
  72.  
  73.         End Try
  74.  
  75.     End Sub
  76.  
  77.  
  78. End Class
Mar 21 '07 #2
kenobewan
4,871 Expert 4TB
Well done! Thanks for sharing the solution.
Mar 22 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: John Flynn | last post by:
hi, having problems reading from and writing back to the same file. basically, i want to read lines of text from a file and reverse them and write them back to the same file.. it has to...
1
by: Magix | last post by:
Hi, I have these string data: str_data1, str_data2, str_data3, which capture some value after a routine process A. Then I would like to write (append) these 3 string values into a text file each...
1
by: Jim Heimer | last post by:
Hi, I've been using schema.ini to read an ascii file to a dataset. Would it be possible to write to the ascii file the same way I read from it? In my case, the initial ascii file has two...
9
by: msuk | last post by:
All, I have a well form block of XML that is stored in a C# string type and I just simply want to display it in the browser using Response.Write but when I try this I get the following error: ...
10
by: Mike | last post by:
I have a text file that i need to write multiple lines of data to but one at a time. how can i write more then one line of data to the file? example: i do a search for a file on a server if the...
12
by: Nina | last post by:
Hi there, What is the maximum length that one line can hold in a text file using StreamWriter's Write or WriteLine method? If the string is too long for one line to hold, what will Write or...
4
by: Billy | last post by:
Hi all, I'm building a text file from a database table using the ASP Write Method and would like to position the cursor in a specific column position before writing the fields. As I loop through...
6
Atran
by: Atran | last post by:
Hello: In this article: You will learn to Write or Read A Text File. Let's Begin: First Create a new project (ConsoleApp or WinApp). And Make sure your program uses these namespaces: using...
6
by: globalrev | last post by:
i ahve a program that takes certain textsnippets out of one file and inserts them into another. problem is it jsut overwrites the first riow every time. i want to insert every new piece of...
65
by: Hongyu | last post by:
Dear all: I am trying to write to a file with full directory name and file name specified (./outdir/mytestout.txt where . is the current directory) in C programming language and under Unix, but...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.