Solved

appending an xml from vb.net

Posted on 2006-07-18
4
280 Views
Last Modified: 2010-04-23
Experts

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:

<?xml version="1.0" encoding="UTF-8"?>
<MTTConfig>
  <configList software="Haulage">
    <companyConfig>
      <companyName>Joes Garage</companyName>
      <dbServerConfig>
        <dbServer>PostgreSQL</dbServer>
        <serverAddress>localhost</serverAddress>
        <port>5432</port>
        <database>mtt</database>
        <user>postgresmtt</user>
      </dbServerConfig>
    </companyConfig>
    <companyConfig>
      <companyName>test 1</companyName>
      <dbServerConfig>
        <dbServer>PostgreSQL</dbServer>
        <serverAddress>ip add</serverAddress>
        <port>5432</port>
        <database>mtt</database>
      </dbServerConfig>
    </companyConfig>
    <companyConfig>
      <companyName>Freds Garage</companyName>
      <dbServerConfig>
        <dbServer>PostgreSQL</dbServer>
        <serverAddress>ipadd</serverAddress>
        <port>5432</port>
        <database>mtt</database>
        <user>postgresmtt</user>
      </dbServerConfig>
    </companyConfig>
    <companyConfig>
      <companyName>Bills Garage</companyName>
      <dbServerConfig>
        <dbServer>SQLServer</dbServer>
        <serverAddress>127.0.0.1</serverAddress>
        <port>1433</port>
        <database>MTT</database>
        <misc>Trusted_Connection=false</misc>
      </dbServerConfig>
    </companyConfig>
    <companyConfig>
      <companyName>Alfs Garage</companyName>
      <dbServerConfig>
        <dbServer>PostgreSQL</dbServer>
        <serverAddress>ipadd</serverAddress>
        <port>5432</port>
        <database>mtt</database>
        <user>mtt1</user>
      </dbServerConfig>
    </companyConfig>
  </configList>
 <companyConfig>
    <companyName>test</companyName>
    <serverAddress>765675</serverAddress>
    <port>8768</port>
    <database>mtt</database>
  </companyConfig>
</MTTConfig>

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)

            xmlChildEl = xmldoc.CreateElement("serverAddress")
            xmlChildEl.AppendChild(xmldoc.CreateTextNode(srv_txt.Text))
            xmlEl.AppendChild(xmlChildEl)

            xmlChildEl = xmldoc.CreateElement("port")
            xmlChildEl.AppendChild(xmldoc.CreateTextNode(port_txt.Text))
            xmlEl.AppendChild(xmlChildEl)

            xmlChildEl = xmldoc.CreateElement("database")
            xmlChildEl.InnerText = db_txt.Text
            xmlEl.AppendChild(xmlChildEl)

            xmldoc.Save(gc.HmDir & "Company2.xml")

Regards

Chas
0
Comment
Question by:chas805
  • 2
  • 2
4 Comments
 
LVL 14

Expert Comment

by:Ramuncikas
Comment Utility
 ...
  Dim xConfigList as XMLElement
  xConfigList = xmldoc.SelectSingleNode("/MTTConfig")
 
  Dim xmlEl As XmlElement = xmldoc.CreateElement("companyConfig")
  xConfigList.AppendChild(xmlEl) 'Append to company config collection
  ...
0
 

Author Comment

by:chas805
Comment Utility
Thanks for reply Ramuncikas

But no joy I am afraid adds it to the same place within the xml document at the end.

Regards

Chas
0
 
LVL 14

Accepted Solution

by:
Ramuncikas earned 500 total points
Comment Utility
               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
 

Author Comment

by:chas805
Comment Utility
Thanks Ramuncikas

Just the trick.

Regards

Chas
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now