I am attempting to append a new entry to an xml file that stores connections, however when I write the new data to the xml file is does not wuite add it to the correct place within the xml file. The file I am appending is as below:
The part I have just appended is the last lot of company details after </configList>, these details should be added before the </configList> however. I can not seem do do this. .net code I am using is as below, perhaps by looking at this you can point me int he right direction.
Dim xmldoc As New XmlDocument
xmldoc.Load(gc.HmDir & "Company2.xml")
Dim xmlEl As XmlElement = xmldoc.CreateElement("companyConfig")
xmldoc.DocumentElement.AppendChild(xmlEl) 'Append to company config collection
Dim xmlChildEl As XmlElement
xmlChildEl = xmldoc.CreateElement("companyName")
Dim xmlText As XmlText = xmldoc.CreateTextNode(con_txt.Text)
xmlChildEl.AppendChild(xmlText)
xmlEl.AppendChild(xmlChildEl)
Dim xmldoc As New XmlDocument
xmldoc.Load("c:\test.xml")
Dim xConfigElement As XmlElement
xConfigElement = xmldoc.SelectSingleNode("/MTTConfig/configList") '<<== here I made mistake myself; sorry
Dim xmlEl As XmlElement = xmldoc.CreateElement("companyConfig")
xConfigElement.AppendChild(xmlEl) 'Append to company config collection
Dim xmlChildEl As XmlElement
xmlChildEl = xmldoc.CreateElement("companyName")
Dim xmlText As XmlText = xmldoc.CreateTextNode("Test string for company name")
xmlChildEl.AppendChild(xmlText)
xmlEl.AppendChild(xmlChildEl)
xmlChildEl = xmldoc.CreateElement("serverAddress")
xmlChildEl.AppendChild(xmldoc.CreateTextNode("Test string for server address"))
xmlEl.AppendChild(xmlChildEl)
xmlChildEl = xmldoc.CreateElement("port")
xmlChildEl.AppendChild(xmldoc.CreateTextNode("Test string for port"))
xmlEl.AppendChild(xmlChildEl)
xmlChildEl = xmldoc.CreateElement("database")
xmlChildEl.InnerText = "Test string for database"
xmlEl.AppendChild(xmlChildEl)
xmldoc.Save("c:\test.xml")
0
chas805Author Commented:
Thanks Ramuncikas
Just the trick.
Regards
Chas
0
Question has a verified solution.
Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.
ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.
One of a set of tools we're offering as a way to say thank you for being a part of the community.
Dim xConfigList as XMLElement
xConfigList = xmldoc.SelectSingleNode("/
Dim xmlEl As XmlElement = xmldoc.CreateElement("comp
xConfigList.AppendChild(xm
...