Results 1 to 4 of 4

Thread: [2005] Adding data to an already existing XML file?

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    [2005] Adding data to an already existing XML file?

    I am having some trouble working with XML files. I have some code that checks if the XML file exists and if not, creates it. If it does exist, it simply loads it. That works fine. Here's the code for that

    If Not System.IO.File.Exists("C:\temp\test.xml") Then
    'XMLDoc = New XmlDocument()
    Dim XMLDeclaration As XmlDeclaration = XMLDoc.CreateXmlDeclaration("1.0", "utf-8", "")
    Dim XMLRootNode As XmlElement = XMLDoc.CreateElement("CDLIST")
    XMLDoc.InsertBefore(XMLDeclaration, XMLDoc.DocumentElement)
    XMLDoc.AppendChild(XMLRootNode)
    XMLDoc.Save("C:\temp\test.xml")
    Else
    XMLDoc.Load("C:\temp\test.xml")
    End If

    Now, what I want it do is add a section that looks like this

    <CD>
    <TITLE>Empire Burlesque</TITLE>
    <ARTIST>Bob Dylan</ARTIST>
    <COUNTRY>USA</COUNTRY>
    <COMPANY>Columbia</COMPANY>
    <PRICE>10.90</PRICE>
    <YEAR>1985</YEAR>
    </CD>

    This code sort of works but doesn't really What am I doing wrong?
    Dim ParentNode As XmlElement = XMLDoc.DocumentElement.PrependChild(XMLDoc.CreateElement("TEST"))

    ParentNode.AppendChild(XMLDoc.CreateElement("Owner"))
    ParentNode.AppendChild(XMLDoc.CreateTextNode("asdasd"))
    ParentNode.AppendChild(XMLDoc.CreateElement("type"))
    ParentNode.AppendChild(XMLDoc.CreateTextNode("123213"))
    ParentNode.AppendChild(XMLDoc.CreateElement("test"))
    ParentNode.AppendChild(XMLDoc.CreateTextNode("kouewo"))

  2. #2

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    Re: [2005] Adding data to an already existing XML file?

    Ah OK, this works but it seems a little "clunky", is there a better way or is this correct?

    Dim ParentNode As XmlElement = XMLDoc.DocumentElement.PrependChild(XMLDoc.CreateElement("TEST"))
    Dim OwnerElement As XmlElement = ParentNode.AppendChild(XMLDoc.CreateElement("Owner"))
    OwnerElement.AppendChild(XMLDoc.CreateTextNode("asdasd"))

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2006
    Posts
    746

    Re: [2005] Adding data to an already existing XML file?

    Anyone?

  4. #4
    Frenzied Member
    Join Date
    Sep 2005
    Posts
    1,547

    Re: [2005] Adding data to an already existing XML file?

    Quote Originally Posted by Ginolard
    Anyone?
    well u could always extract all the data and rebuild the xml...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width