Android Question Unintentional conversion of & to & by textwriter?

Penfound

Active Member
Licensed User
Longtime User
I have an XML file that is read parsed into an SQLLite DB. At some point the user can add a new record to the DB and then the XML file is re-written using Textwriter.

This works perfectly unless there is an & in the original XML file and subsequent DB. Somewhere in the conversion process it gets change to "&" in the new XML file which then causes a failure.

B4X:
Sub btnSpecial_Click
 lstHours.Initialize("lstHours")
 lstMinutes.Initialize("lstMinutes")
 Dim LastID As Int
 If tmpTime = "" Then tmpTime = "11:00"
 If txtTitle.Text = "" Then
 Msgbox("You need a title","Error!")
 Else
 Title = txtTitle.text
 End If
 If txtDescription.text = "" Then
 Description = "Not Entered"
 Else
 Description = txtDescription.text
 End If
 If txtLocation.Text = "" Then
 EventLocation = "Not Entered"
 Else
 EventLocation = txtLocation.text
 End If
 Postcode = "Unknown"
 Notes = "None"
 LastX = SQL1.ExecQuerySingleResult("SELECT count(*) FROM table1")
 EventID = LastX + 1
 EventTime = tmpTime
 DateTime.DateFormat = "dd MMM yyyy"
 EventDate = DateTime.Date(ADP.selectedDate)
 EventMonth = DateUtils.GetMonthName(ADP.selectedDate)
 EventDay = DateUtils.GetDayOfWeekName(ADP.selectedDate)

 txt="INSERT INTO table1 VALUES(" & EventID & ",'" & EventMonth & "','" & EventType & "','" & Status & "','" & EventDay & "','" & EventDate & "','" & EventTime & "','" & Title & "','" & Description & "','" & EventLocation & "','" & Postcode & "','" & Notes & "','" & Band1Name & "')"
 Cursor1 = SQL1.ExecQuery(txt)
 ToastMessageShow("Entry added", False) ' confirmation for the user
 
 LastX = SQL1.ExecQuerySingleResult("SELECT count(*) FROM table1")
 
 Cursor1.Close 
 Activity.Title = "Add New Entry: " & LastX
 ReWriteXML
End Sub

Sub ReWriteXML
'add the event back into the XML file and Upload the file Wow!
 txt = "SELECT * FROM table1 ORDER BY EventDate ASC"
 Cursor1 = SQL1.ExecQuery(txt)
  TextWriter1.Initialize(File.OpenOutput(File.DirRootExternal, "bbdiary.xml",False))
 Line = "<?xml version='1.0' standalone='yes'?>" & CRLF
 Line = Line & "<NewDataSet>"
 WriteXMLFile(Line)
 
For i = 0 To Cursor1.RowCount - 1
 Cursor1.Position = i 
 EventType = Cursor1.GetString("EventType")
 Status = Cursor1.GetString("Status")
 EventMonth = Cursor1.GetString("EventMonth")
 EventDay = Cursor1.GetString("EventDay")
 EventDate = Cursor1.GetString("EventDate")
 EventTime = Cursor1.GetString("EventTime")
 Title = Cursor1.GetString("Title")
 Description = Cursor1.GetString("Description")
 EventLocation = Cursor1.GetString("EventLocation")
 Postcode = Cursor1.GetString("Postcode")
 Notes = Cursor1.GetString("Notes")
 BandName = Cursor1.GetString("BandName")
 Line = "  <EventsDiary>" & CRLF
 Line = Line & "  <EventID>" & i & "</EventID>" & CRLF
 Line = Line & "  <EventType>" & EventType & "</EventType>" & CRLF
 Line = Line & "  <EventStatus>" & Status & "</EventStatus>" & CRLF
 Line = Line & "  <EventMonth>" & EventMonth & "</EventMonth>" & CRLF
 Line = Line & "  <EventDay>" & EventDay & "</EventDay>" & CRLF
 Line = Line & "  <EventDate>" & EventDate & "</EventDate>" & CRLF
 Line = Line & "  <EventTime>" & EventTime & "</EventTime>" & CRLF
 Line = Line & "  <Title>" & Title & "</Title>" & CRLF
 Line = Line & "  <Description>" & Description & "</Description>" & CRLF
 Line = Line & "  <EventLocation>" & EventLocation & "</EventLocation>" & CRLF
 Line = Line & "  <Postcode>" & Postcode & "</Postcode>" & CRLF
 Line = Line & "  <Notes>" & Notes & "</Notes>" & CRLF
 Line = Line & "  <BandName>" & BandName & "</BandName>" & CRLF
 Line = Line & "  </EventsDiary>"
 WriteXMLFile(Line)
 Next
 Line = "</NewDataSet>"
 WriteXMLFile(Line)
 TextWriter1.Flush
  TextWriter1.Close
 ToastMessageShow("Written XML File",False)
 SQL1.close
 File.Copy(File.DirRootExternal,"/bbdiary.xml",File.DirDefaultExternal,"/bbdiary.xml")
 parser.Initialize
 SQL1.Initialize(File.DirDefaultExternal, "/bbdiary.db", True)
 CreateTables
 ProgressDialogHide
 ToastMessageShow("Files Updated!",False)
 Splash_Complete
End Sub
Sub WriteXMLFile(NewLine As String)
 TextWriter1.WriteLine(NewLine)
End Sub

Original XML lines
<Description />
<EventLocation>Parkhouse Centre Rooms 2 &amp; 3</EventLocation>
<Postcode />

Error Lines

<Description></Description>
<EventLocation>Parkhouse Centre Rooms 2 & 3</EventLocation>
<Postcode></Postcode>

Thanks for looking
Penfound
 
Top