Append XML into an existing document

csharp Mexico
  • 13 years ago
    Hi, i have a problem, and hope you can help me! I know how to append xml data using xmldocument at the end of the xml file, but i need append xml data between nodes, yes !!at specific location!! depending on the atrribute of a xml node like a database key. Thank you for your help =)
  • 13 years ago
    Dear Cesar,
      I herewith furnished one subroutine for how to append/modify the exising section in the web.config file. The same concept is need to modify/append the xml document.




    Public Shared Sub WriteSetting(ByVal key As String, ByVal value As String)
            Dim doc As New XmlDocument
            doc.Load(AppDomain.CurrentDomain.BaseDirectory & "web.config")
            Dim node As XmlNode = doc.SelectSingleNode("/configuration/appSettings")
            If node Is Nothing Then
                'Throw New InvalidOperationException("appSettings section not found in config file.")
            End If
            Try
                Dim elem As XmlElement = CType(node.SelectSingleNode(String.Format("add[@key='{0}']", key)), XmlElement)
                If Not (elem Is Nothing) Then
                    elem.SetAttribute("value", value)

                Else
                    elem = doc.CreateElement("add")
                    elem.SetAttribute("key", key)
                    elem.SetAttribute("value", value)
                    node.AppendChild(elem)
                End If
                doc.Save(AppDomain.CurrentDomain.BaseDirectory & "web.config")

            Catch
                Throw
            End Try
        End Sub

























    I hope this above concept may solve your problem.

    Good Luck

    Hari K......






Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“You can stand on the shoulders of giants OR a big enough pile of dwarfs, works either way.”