Solved

appending an xml from vb.net

Posted on 2006-07-18
4
287 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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

This article explains how to create and use a custom WaterMark textbox class.  The custom WaterMark textbox class allows you to set the WaterMark Background Color and WaterMark text at design time.   IMAGE OF WATERMARKS STEPS Create VB …
If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

813 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

19 Experts available now in Live!

Get 1:1 Help Now