I'm trying to overwrite an existing XML file. If the file exists it doesn't get overwritten. Here I check for it. Why doesn't it get overwritten. What am I missing?
Here is my code
Dim settings As New XmlWriterSettings()
settings.Indent = True
Dim FilePath As String = "\\hqdadev02\sigar_public$\rss\alert-special-reports.xml"
If Not File.Exists(FilePath) Then
'File.Copy(FilePath, overwrite)
Dim writer As XmlWriter = XmlWriter.Create("\\hqdadev02\sigar_public$\rss\alert-special-reports.xml", settings)
writer.WriteStartDocument()
writer.WriteStartElement("rss")
writer.WriteAttributeString("version", "2.0")
writer.WriteStartElement("channel")
writer.WriteElementString("title", "SIGAR | Audits | Alert and Special Reports")
writer.WriteElementString("link", "http://www.sigar.mil/audits/alert-special-reports.html")
writer.WriteElementString("description", "SIGAR | Audits | Alert and Special Reports")
While dr.Read()
writer.WriteStartElement("item")
writer.WriteElementString("title", dr.GetString(dr.GetOrdinal("Title")) + " (PDF)")
writer.WriteElementString("link", dr.GetString(dr.GetOrdinal("LinkPath").ToString()))
writer.WriteElementString("description", dr.GetString(dr.GetOrdinal("Description")))
writer.WriteElementString("category", "Alert and Special Reports")
writer.WriteElementString("pubDate", String.Format("{0:R}", dr("PubDate")))
writer.WriteEndElement()
End While
writer.WriteEndElement()
writer.WriteEndElement()
writer.WriteEndDocument()
writer.Flush()
writer.Close()
dr.Close()
End If
Your if statement checks to see if the file doesn't exist. If it doesn't, then that's the only time your code for saving the file will execute.
You might try: IfFile.Exists(FilePath)Then
File.Delete(FilePath)
End If
This would delete the file and allow you to write to it. Sometimes it's easier to delete a file as you can run into an issue whereby if the data you are saving is shorter than the current file, the previous data exists beyond that point. This would require
an extra step where you would open a filehandle with the truncate option to automatically trim away the extra. In this case though, deleting the file will ensure that you are able to save your data.
Don't forget to mark useful responses as Answer if they helped you towards a solution.
In order to resolve your issue, could you simply your project and upload to skydrive? Then provide the link. I want to test locally and try to find the issue.
Best wishes,
Please mark the replies as answers if they help or unmark if not.
Feedback to us
deongee
Member
21 Points
128 Posts
how to overwrite an existing file using XMLWriter
Nov 19, 2012 09:49 PM|LINK
I'm trying to overwrite an existing XML file. If the file exists it doesn't get overwritten. Here I check for it. Why doesn't it get overwritten. What am I missing?
Here is my code
Dim settings As New XmlWriterSettings() settings.Indent = True Dim FilePath As String = "\\hqdadev02\sigar_public$\rss\alert-special-reports.xml" If Not File.Exists(FilePath) Then 'File.Copy(FilePath, overwrite) Dim writer As XmlWriter = XmlWriter.Create("\\hqdadev02\sigar_public$\rss\alert-special-reports.xml", settings) writer.WriteStartDocument() writer.WriteStartElement("rss") writer.WriteAttributeString("version", "2.0") writer.WriteStartElement("channel") writer.WriteElementString("title", "SIGAR | Audits | Alert and Special Reports") writer.WriteElementString("link", "http://www.sigar.mil/audits/alert-special-reports.html") writer.WriteElementString("description", "SIGAR | Audits | Alert and Special Reports") While dr.Read() writer.WriteStartElement("item") writer.WriteElementString("title", dr.GetString(dr.GetOrdinal("Title")) + " (PDF)") writer.WriteElementString("link", dr.GetString(dr.GetOrdinal("LinkPath").ToString())) writer.WriteElementString("description", dr.GetString(dr.GetOrdinal("Description"))) writer.WriteElementString("category", "Alert and Special Reports") writer.WriteElementString("pubDate", String.Format("{0:R}", dr("PubDate"))) writer.WriteEndElement() End While writer.WriteEndElement() writer.WriteEndElement() writer.WriteEndDocument() writer.Flush() writer.Close() dr.Close() End Ifmarkfitzme
Star
14319 Points
2215 Posts
Re: how to overwrite an existing file using XMLWriter
Nov 19, 2012 10:27 PM|LINK
Your if statement checks to see if the file doesn't exist. If it doesn't, then that's the only time your code for saving the file will execute.
You might try:
If File.Exists(FilePath) Then
File.Delete(FilePath)
End If
This would delete the file and allow you to write to it. Sometimes it's easier to delete a file as you can run into an issue whereby if the data you are saving is shorter than the current file, the previous data exists beyond that point. This would require an extra step where you would open a filehandle with the truncate option to automatically trim away the extra. In this case though, deleting the file will ensure that you are able to save your data.
deongee
Member
21 Points
128 Posts
Re: how to overwrite an existing file using XMLWriter
Nov 19, 2012 11:12 PM|LINK
Not working
Catherine Sh...
All-Star
23373 Points
2490 Posts
Microsoft
Re: how to overwrite an existing file using XMLWriter
Nov 23, 2012 06:51 AM|LINK
Hi,
In order to resolve your issue, could you simply your project and upload to skydrive? Then provide the link. I want to test locally and try to find the issue.
Best wishes,
Feedback to us
Develop and promote your apps in Windows Store
deongee
Member
21 Points
128 Posts
Re: how to overwrite an existing file using XMLWriter
Nov 23, 2012 04:19 PM|LINK
The issue was not the code, but a permission for IUSR account. My server admin fixed it. Thanks