locked
Opening empty file as XMLDocument RRS feed

  • 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 If

    gXmlData.Load(FICHIER)

    End Sub

    Thanks 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 PM
    Moderator

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 PM
    Moderator
  • 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 PM
    Moderator
  • 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 PM
    Moderator
  • 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 PM
    Moderator
  • Always get an XML exception with this
    Wednesday, February 22, 2006 8:33 PM
  • that does it ! Thanks a lot
    Wednesday, 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 PM
    Moderator
  • You are welcome! Please, post your problem as soon as you have a new one
    Wednesday, February 22, 2006 8:38 PM
    Moderator
  • 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 PM
    Moderator
  • 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 PM
    Moderator