Hi,
I am working with a xml file.
I use writexml to write into that xml and this is saved on the local machine.
When i next write to that xml file, i want to delete the file and create a new xml file with the same name.
How can i do this? can anyone help me with this?
Thanks in Advance |
| rowter 19 hours 39 minutes ago |
public static void Delete(string path) Member of System.IO.File Summary: Deletes the specified file. An exception is not thrown if the specified file does not exist. Parameters: path: The name of the file to be deleted. There are a couple of classes in the System.IO namespace that you should explore. File, Directory, Path. There are Types in other namespaces that derive from other classes in the System.IO namespace. Stream, TextWriter, TextReader.
Mark the best replies as answers. "Fooling computers since 1971."- Marked As Answer byrowter 14 hours 56 minutes ago
- Edited byRudedog2 14 hours 51 minutes ago
-
|
| Rudedog2 14 hours 59 minutes ago |
How are you creating the file intially? Post the code. It might already be taken care of if you initialize a streamwriter object with the System.IO.FileMode enum set to OpenOrCreate.
Mark the best replies as answers. "Fooling computers since 1971." |
| Rudedog2 18 hours 33 minutes ago |
Dim xmlpath As String = "C:\Column Header.xml"
col_ds.WriteXml(xmlpath, XmlWriteMode.IgnoreSchema) I am writing to this xml file at this location.
Now if i have to write again tothis file, i either want t delete this file and create a new one with the same name or clear the contents of this file and write again.
thanks in advance
|
| rowter 17 hours 40 minutes ago |
Haven't you tried just simply ignoring the fact that a file might already exist, and just simply run the code to see what happens? Mark the best replies as answers. "Fooling computers since 1971." |
| Rudedog2 17 hours 29 minutes ago |
I will rewrite the same data in a different order in that xml file and will have to read that data Running the code as it is:
It is appending the data at the end of the file. So, i am getting the same set of data twice when i read from the xml later on. This is causing some issues.
|
| rowter 17 hours 14 minutes ago |
What type of object, col_ds , is actually writing the file? You appear to be using a dataset, and a dataset's WriteXml method does not append to files that I am aware of. It simply writes the contents of all the tables that are defined at that time as XML. If a dataset appended onto a file, then it would not be writing out the content of the tables at all. How is the data being initially loaded into the Dataset? If you see duplicated data in the Xml file that it writes, then duplicated data must be getting entered into it at some point. The dataset is writing duplicate data because it contains duplicate data. You can prove or disprove this by setting a breakpoint on the line where you write the data. But, you yet claim if you delete the file first it does not write duplicate entries. That makes no sense. And please do not tell me that you have not yet tried to delete the file first.
Mark the best replies as answers. "Fooling computers since 1971." |
| Rudedog2 16 hours 23 minutes ago |
Hi rudedgog,
thanks for pointing me towards dataset. Now it works. I did not get a chance to check the dataset if it has duplicate data. all i did was add ds.clear before writing to xml.
This works perfect now. I do not want to mess with the code right now. i will worry about it later.
one question. if you wat to delete the xml file file through code how do you do it? i have no idea.
Thanks |
| rowter 15 hours 4 minutes ago |
public static void Delete(string path) Member of System.IO.File Summary: Deletes the specified file. An exception is not thrown if the specified file does not exist. Parameters: path: The name of the file to be deleted. There are a couple of classes in the System.IO namespace that you should explore. File, Directory, Path. There are Types in other namespaces that derive from other classes in the System.IO namespace. Stream, TextWriter, TextReader.
Mark the best replies as answers. "Fooling computers since 1971."- Marked As Answer byrowter 14 hours 56 minutes ago
- Edited byRudedog2 14 hours 51 minutes ago
-
|
| Rudedog2 14 hours 59 minutes ago |
Thanks |
| rowter 14 hours 56 minutes ago |
Please note that the method does not provide any feedback as to success or failure, and it does not throw an exception. Mark the best replies as answers. "Fooling computers since 1971." |
| Rudedog2 12 hours 11 minutes ago |