Important alert: (current site time 7/15/2013 8:09:23 AM EDT)
 

VB icon

Add carriage return/line feed to XML

Email
Submitted on: 9/26/2002 3:57:21 PM
By: Robert J May 
Level: Beginner
User Rating: By 1 Users
Compatibility: VB.NET
Views: 47207
(About the author)
 
     This is a very simple console application that opens an XML based file, searches for the ">" (which signifies the end of an element) and adds a carriage return and line feed. Sometimes, when output XML files, the text can be one big long line. Internet Explorer and Visual Studio have problems with long lines (they go slow), so this allows you to break up the long lines.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: Add carriage return/line feed to XML
// Description:This is a very simple console application that opens an XML based file, searches for the ">" (which signifies the end of an element) and adds a carriage return and line feed.
Sometimes, when output XML files, the text can be one big long line. Internet Explorer and Visual Studio have problems with long lines (they go slow), so this allows you to break up the long lines.
// By: Robert J May
//
// Inputs:/? : Help
/F : File name to process
/N : New filename to create
/O : Overwrite the new file if it exists
//
// Assumes:Only the module is included. To use, create a new console application. Then, in the module thats created for you, cut and paste the code below, replacing all of the code that is present in the module.
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=588&lngWId=10//for details.//**************************************

Imports System.IO
Module modAddCrlf
	Private m_bOverwrite As Boolean
#Region "Enums"
	Private Enum Actions
		HELP = 0
		COMMANDLINEINVALID = 1
		ADDCRLF = 2
	End Enum
	Private Enum CommandLineArgs
		FILENAME = Asc("F")
		NEWFILENAME = Asc("N")
		OVERWRITE = Asc("O")
		HELP = Asc("?")
	End Enum
#End Region
	Sub Main()
		Dim iCommand As Actions
		Dim sOldFileName As String
		Dim sNewFileName As String
		Console.WriteLine()
		If Trim(Command()) = "" Then
			iCommand = Actions.HELP
		Else
			iCommand = ParseCommandLine(sOldFileName, sNewFileName)
		End If
		Select Case iCommand
			Case Actions.COMMANDLINEINVALID
				Console.WriteLine("Invalid command line. Please use the /? parameter to view help.")
			Case Actions.HELP
				DisplayHelp()
			Case Actions.ADDCRLF
				If ValidateFileNames(sOldFileName, sNewFileName) Then
					Console.WriteLine("Processing " & sOldFileName & " . . .")
					AddCRLF(sOldFileName, sNewFileName)
					Console.WriteLine(sNewFileName & " created.")
				End If
				Console.WriteLine("File processing finished.")
		End Select
	End Sub
	Private Function ParseCommandLine(ByRef r_sOldFileName As String, ByRef r_sNewFileName As String) As Actions
		Dim sArgs As String()
		Dim i As Integer
		Dim iArg As Integer
		Dim sValue As String
		Dim iCommand As Actions
		Dim sOldFileName As String
		Dim sNewFileName As String
		Dim bFilenameProvided As Boolean
		iCommand = Actions.ADDCRLF
		sArgs = Split(Command(), "/")
		For i = 1 To UBound(sArgs)
			If Trim(sArgs(i)) <> "" Then
				iArg = Asc(UCase(Left(sArgs(i), 1)))
				Select Case iArg
					Case CommandLineArgs.OVERWRITE
						m_bOverwrite = True
					Case CommandLineArgs.FILENAME
						sOldFileName = Trim(Right(sArgs(i), Len(sArgs(i)) - 1))
						bFilenameProvided = True
					Case CommandLineArgs.NEWFILENAME
						sNewFileName = Trim(Right(sArgs(i), Len(sArgs(i)) - 1))
					Case CommandLineArgs.HELP
						iCommand = Actions.HELP
						Exit For
					Case Else
						iCommand = Actions.COMMANDLINEINVALID
						Exit For
				End Select
			Else
				iCommand = Actions.COMMANDLINEINVALID
				Exit For
			End If
		Next
		If bFilenameProvided = False And iCommand = Actions.ADDCRLF Then
			Console.WriteLine("You must provide a filename using the /F option.")
			iCommand = Actions.COMMANDLINEINVALID
		End If
		r_sOldFileName = sOldFileName
		r_sNewFileName = sNewFileName
		ParseCommandLine = iCommand
	End Function
	Private Function ValidateFileNames(ByRef r_sOldFileName As String, ByRef r_sNewFileName As String) As Boolean
		Dim bValid As Boolean
		bValid = True
		If Not File.Exists(r_sOldFileName) Then
			Console.WriteLine(r_sOldFileName & " does not exist!")
			bValid = False
		End If
		If Trim(r_sNewFileName) = "" Then
			r_sNewFileName = r_sOldFileName & ".1"
		End If
		If File.Exists(r_sNewFileName) Then
			If m_bOverwrite = True Then
				File.Delete(r_sNewFileName)
			Else
				Console.WriteLine(r_sNewFileName & " already exists. To overwrite, use the /o option.")
				bValid = False
			End If
		End If
		If r_sNewFileName = r_sOldFileName Then
			Console.WriteLine("The filenames are the same (" & r_sNewFileName & "). Please select different filenames.")
		End If
		ValidateFileNames = bValid
	End Function
	Private Sub AddCRLF(ByVal p_sOldFilename As String, ByVal p_sNewFileName As String)
		'this is the function that actually does all of the work.
		'rather than reading the entire file into a buffer, we read only on character at a 
		'time. It's not much slower (if slower at all), and allows us to parse files
		'of any size.
		Dim srFile As StreamReader
		Dim swFile As StreamWriter
		Dim sChar(0) As Char
		Dim i As Integer
		srFile = File.OpenText(p_sOldFilename)
		swFile = File.CreateText(p_sNewFileName)
		i = 1
		Do While i = 1
			i = srFile.ReadBlock(sChar, 0, 1)
			If sChar(0).ToString = ">" Then
				swFile.WriteLine(sChar)
			Else
				swFile.Write(sChar)
			End If
		Loop
		srFile.DiscardBufferedData()
		srFile.Close()
		swFile.Close()
	End Sub
	Private Sub DisplayHelp()
		Console.WriteLine("This utility will add a carriage return and line feed onto the right of every")
		Console.WriteLine("element found in the XML document.")
		Console.WriteLine("This is useful for VS.NET and IE, since both have problems with long lines in")
		Console.WriteLine("XML files.")
		Console.WriteLine("Robert May, 9/26/02")
		Console.WriteLine()
		Console.WriteLine("Options")
		Console.WriteLine("----------------------------------------------------")
		Console.WriteLine("/?	:	This help screen.")
		Console.WriteLine("/F	:	Filename to add the CRLFs.")
		Console.WriteLine("/N	:	New filename to be created from old.")
		Console.WriteLine("/O	:	Overwrite an existing file.")
	End Sub
End Module


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Beginner category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments

7/29/2003 2:11:38 AM

It seem that it does the job , but the xml file that it produces is not valid for some reason... any ideas?
(If this comment was disrespectful, please report it.)

 
7/29/2003 10:44:18 AMRobert J May

When you say that the file isn't valid, is the file valid before it goes in? What is reported as being invalid by the parser? I've never had it make a file invalid before, so any additional information would be helpful.
(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.