StreamReader > XMLTextWriter Problems

Results 1 to 2 of 2

Thread: StreamReader > XMLTextWriter Problems

  1. #1
    Join Date
    Dec 1969

    Default StreamReader > XMLTextWriter Problems

    I am in my early stages .Net development.<BR>I am trying to read a text file from another server and write the contents to a new formatted XML document. The problem I have encountered has me stumped and I cannot see a way to proceed further until I overcome it. I am able to write the first 3 blocks of data to the XML file without problem but when I hit the third line the stream appears to end.<BR><BR>The test file I am reading from can be viewed at:<BR><BR><BR>The XML output can be viewed at:<BR><BR><BR>Please see below for the extract of my source code:<BR>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~<BR>
    <BR>Sub btnWriteXML_OnClick(sender As Object, e As EventArgs)<BR>	<BR>	Dim newRobot = 0<BR>	&#039;enc is encoding object required by constructor<BR>	&#039;If It is null, so default encoding is used	<BR>	Dim enc as Encoding<BR>	<BR>	Dim objXMLWriter as new XMLTextWriter(Server.MapPath("/projects/robots/robots.xml"), enc)<BR>	&#039;Define some formatting in the XML document for readability<BR>	objXMLWriter.Formatting = Formatting.Indented<BR>	objXMLWriter.Indentation = 2<BR><BR>	Try<BR>	<BR>		Dim myRequest as HttpWebRequest <BR>		Dim myResponse as HttpWebResponse <BR>		Dim sr as StreamReader <BR>		Dim line as String <BR>		Dim newLine<BR>		<BR>		&#039;Writes the document declaration with an XML version of 1<BR>		objXMLWriter.WriteStartDocument()<BR>		&#039;Write out a comment and time stamp at the top of the document<BR>		objXMLWriter.WriteComment("Created by - " & Now())<BR>		&#039;Top level (Parent element)<BR>		objXMLWriter.WriteStartElement("robots_txt")<BR>	<BR>		myRequest = CType(WebRequest.Create(""), HttpWebRequest) <BR>		myResponse = CType(myRequest.GetResponse(), HttpWebResponse)<BR>		<BR>		sr = New StreamReader(myResponse.GetResponseStream()) <BR>		line = sr.ReadLine()  <BR>		<BR>		Do While sr.Peek() &#062; -1 or line &#060;&#062; "" <BR>			&#039; Check the string is not empty<BR>			If line.Length &#062; 0 Then<BR>			  			<BR>				&#039;Split the string to remove the unwanted labels at the start of each line <BR>				newline = Split(line, ": ")<BR>				<BR>				&#039;Check if we are starting new Robot details, if so set the new robot flag<BR>				If inStr(newline(0), "robot-id")<BR>					newRobot = 1<BR>				End If<BR>				<BR>				If newRobot = 1 Then<BR>					objXMLWriter.WriteStartElement("robot")<BR>				End If<BR>				<BR>				If newline(1) &#060;&#062; Nothing Then<BR>					objXMLWriter.WriteStartElement(newline(0))<BR>					objXMLWriter.WriteString(newline(1))<BR>					objXMLWriter.WriteEndElement<BR>				Else<BR>					objXMLWriter.WriteElementString(newline(0),"No information currently available")<BR>				End If<BR>				<BR>				&#039;Check if this is the last line of info for this robot and close the element<BR>				If inStr(newline(0),"modified-by") Then<BR>					objXMLWriter.WriteEndElement &#039;Close the robot element<BR>				End If<BR>																		<BR>			End If<BR>		<BR>			newRobot = 0<BR>			&#039;Read the next line<BR>			line = sr.ReadLine<BR>		Loop <BR>		&#039;Close the stream reader<BR>		sr.close <BR>		<BR>		objXMLWriter.WriteEndElement &#039;End top level element<BR>		objXMLWriter.WriteEndDocument &#039;End Document<BR>		&#039;Write the file<BR>		objXMLWriter.Flush <BR>		&#039;Display File Just Created<BR>		ReadXML(Server.MapPath("/projects/robots/robots.xml"))<BR>	<BR>	  Catch Ex as Exception<BR>		<BR>		&#039;Close the Writer<BR>		objXMLWriter.Close<BR> <BR>		<BR>		lblXMLFile.Text = "The following error occurred: " & Ex.Message<BR>  End Try<BR>	<BR>	lblXMLFile.Text = "&#060;font color=""#cc0000""&#062;&#060;b&#062;Robots.xml File Created Successfully !&#060;/b&#062;&#060;/font&#062;" & newRobot<BR>End Sub<BR>
    <BR>~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<BR ><BR><BR>Thanks for your help it is much appreciated.

  2. #2

    Default RE: StreamReader > XMLTextWriter Problems

    Have you tested it with a ReadAll to get the complete file from the stream before doing the rest of your processing?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts