Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

appending an xml from vb.net

Posted on 2006-07-18
4
Medium Priority
?
292 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
[X]
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
  • 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 2000 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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

715 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