Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

appending an xml from vb.net

Posted on 2006-07-18
4
288 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
ID: 17128880
 ...
  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
ID: 17128949
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
ID: 17128975
               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
ID: 17129070
Thanks Ramuncikas

Just the trick.

Regards

Chas
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Creating an analog clock UserControl seems fairly straight forward.  It is, after all, essentially just a circle with several lines in it!  Two common approaches for rendering an analog clock typically involve either manually calculating points with…
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…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

839 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