ZeroEffect Posted September 13, 2012 Posted September 13, 2012 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 Quote If you can't find it, Build It. There is no place Like 127.0.0.1 also don't forget 1 + 1 = 10
Administrators PlausiblyDamp Posted September 13, 2012 Administrators Posted September 13, 2012 root.AppendChild(elemTicketIndex) shouldn't that be elem.AppendChild(elemTicketIndex) instead? What version of .Net are you using btw? If you are on .Net 3 or later then there are much nicer ways of working with XML. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
ZeroEffect Posted September 14, 2012 Author Posted September 14, 2012 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. Quote If you can't find it, Build It. There is no place Like 127.0.0.1 also don't forget 1 + 1 = 10
ZeroEffect Posted September 14, 2012 Author Posted September 14, 2012 disregard my question about updating. <facepalm> Quote If you can't find it, Build It. There is no place Like 127.0.0.1 also don't forget 1 + 1 = 10
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.