Solved

appending an xml from vb.net

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
VB.Net - CSV to Oracle table 4 52
Connect to a database from Excel using JDBC instead of ODBC 3 49
Visual Studio editor? 1 38
Crystal reports vb.net 2 23
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…
A while ago, I was working on a Windows Forms application and I needed a special label control with reflection (glass) effect to show some titles in a stylish way. I've always enjoyed working with graphics, but it's never too clever to re-invent …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

863 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

20 Experts available now in Live!

Get 1:1 Help Now