Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

I don't know why I cannot remember how to do this, then again it has been awhile. I am building a XML file I got that down. Now time to added/remove/delete node.

 

Main file looks like this.

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ActiveTickets></ActiveTickets>

 

When a node is added it should look like this.

 

<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<ActiveTickets>
  <ticket>
     <ticketindex>2345</ticketindex>
     <ticketstatus>1</ticketstatus>
     <timestamp>12344567892</timestamp>
  </ticket>
</ActiveTickets>

 

I can get following but I can seem to put it inside the <ticket></ticket> tags.

 

     <ticketindex>2345</ticketindex>
     <ticketstatus>1</ticketstatus>
     <timestamp>12344567892</timestamp>

 

This is my code.

 

       Dim doc As New XmlDocument()
       doc.Load("ActiveTickets.xml")

       Dim root As XmlNode = doc.DocumentElement

       Dim MyNodeTree As Xml.XmlNode

       'Create a new node. 

       Dim elem As XmlElement = doc.CreateElement("Ticket")
       elem.InnerText = ""
       root.AppendChild(elem)

       Dim elemTicketIndex As XmlElement = doc.CreateElement("TicketIndex")
       elemTicketIndex.InnerText = "Ticket Index goes Here"
       root.AppendChild(elemTicketIndex)

       Dim elemStatus As XmlElement = doc.CreateElement("Status")
       elemStatus.InnerText = "Status goes Here"
       root.AppendChild(elemStatus)

       Dim elemTimeStamp As XmlElement = doc.CreateElement("TimeStamp")
       elemTimeStamp.InnerText = "TimeStamp goes Here"
       root.AppendChild(elemTimeStamp)

       doc.Save("ActiveTickets.xml")

 

Thank you for helping me remember.

 

ZeroEffect

If you can't find it, Build It.

 

There is no place Like 127.0.0.1 also don't forget 1 + 1 = 10

Posted

You are correct. I got it figured out last night after walking away for a few.

 

       Dim doc As New XmlDocument()
       doc.Load(XMLPath)

       Dim root As XmlNode = doc.DocumentElement

       'Create a new node. 

       Dim elemTicketIndex As XmlElement = doc.CreateElement("TicketIndex")
       elemTicketIndex.InnerText = "Ticket Index goes Here"
       'root.AppendChild(elemTicketIndex)

       Dim elemStatus As XmlElement = doc.CreateElement("Status")
       elemStatus.InnerText = strStatus
       'root.AppendChild(elemStatus)

       Dim elemTimeStamp As XmlElement = doc.CreateElement("TimeStamp")
       elemTimeStamp.InnerText = strNowPlusOne
       'root.AppendChild(elemTimeStamp)

       Dim elem As XmlElement = doc.CreateElement("Ticket")
       elem.SetAttribute("id", strTicket)
       elem.AppendChild(elemStatus)
       elem.AppendChild(elemTimeStamp)
       'elem.InnerText = ""
       root.AppendChild(elem)

       doc.Save(XMLPath)

 

I have another quick question. How do I update an element? Currently my plan is to delete it and then save it again.

If you can't find it, Build It.

 

There is no place Like 127.0.0.1 also don't forget 1 + 1 = 10

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...