Answered by:
Opening empty file as XMLDocument

Question
-
Hi, in my form load I'm looking for an xml file and if it does not exists then I create it. But wen I try to open it as an XmlDocument it popups an error. I thik its because the created file is empty. Could I add the line "<?xml version="1.0" encoding="utf-8" ?>" to the file on its creation ? and how ?
Here's the code :
Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not File.Exists(FICHIER) Then
MsgBox("Le fichier " + FICHIER + " vient d'être créé donc aucune expressions régulières n'est disponible.", MsgBoxStyle.Information, "Fichier vide")
File.Create(FICHIER).Close()
End IfgXmlData.Load(FICHIER)
End SubThanks for your help !
Wednesday, February 22, 2006 7:49 PM
Answers
-
Just append a new element to it. Here is the modified code:
private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not File.Exists(FICHIER) Then
Dim rootNode As XmlNode = gXmlData.CreateElement("RootNode")
gXmlData.AppendChild(rootNode)
Else
gXmlData.Load(FICHIER)
End If
Wednesday, February 22, 2006 8:32 PMModerator
All replies
-
Try loading it if it exists, or using LoadXML to file your global XMLDocument with an empty XML document.
Wednesday, February 22, 2006 7:51 PMModerator -
So, don't load the XML is the file don't exits and just use a empty XmlDocument. Because when you create a empty XML file and put a Root Node in it, it is still pretty emtpy. So just do this:
private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not File.Exists(FICHIER) Then
MsgBox("Le fichier " + FICHIER + " vient d'être créé donc aucune expressions régulières n'est disponible.", MsgBoxStyle.Information, "Fichier vide")
Else
gXmlData.Load(FICHIER)
End If
End Subclass="txt4">
Now later on you will fill this XmlDocument i geus and later you'll save it. When you save the file will exists after it and next time Form1 will be loaded everything works just fine.
Wednesday, February 22, 2006 7:55 PMModerator -
I tried adding that and adding elements to gxmlData but because gxmlData has no Root Node I Cant. How do I add or specify the root node ?
thanks again
Wednesday, February 22, 2006 8:25 PM -
Do what I said. When you're loading, if there is no file, do gxmlData.LoadXML("<XML/>")
Wednesday, February 22, 2006 8:28 PMModerator -
Just append a new element to it. Here is the modified code:
private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Not File.Exists(FICHIER) Then
Dim rootNode As XmlNode = gXmlData.CreateElement("RootNode")
gXmlData.AppendChild(rootNode)
Else
gXmlData.Load(FICHIER)
End If
Wednesday, February 22, 2006 8:32 PMModerator -
Always get an XML exception with thisWednesday, February 22, 2006 8:33 PM
-
that does it ! Thanks a lotWednesday, February 22, 2006 8:35 PM
-
I can't imagine why - I've been doing that for years. I see your problem is solved though, so that's cool.
Wednesday, February 22, 2006 8:37 PMModerator -
You are welcome! Please, post your problem as soon as you have a new oneWednesday, February 22, 2006 8:38 PMModerator
-
This works fine in C#:
XmlDocument doc =
new XmlDocument();doc.LoadXml("<XML/>");
string s= doc.OuterXml;Does the equivelant not work in VB.NET ?
Wednesday, February 22, 2006 8:42 PMModerator -
I it should work, i tested it here and it worked. Now you have a XmlDocument with a Root Node with the name XML.Wednesday, February 22, 2006 9:26 PMModerator