Link to home
Start Free TrialLog in
Avatar of shanvidhya
shanvidhya

asked on

Add XML Node using another XML file

I have 3 xml files which is stored in one location. I want to add all these 3 XML into one XML file with different node name. I could get the XML values as string using XMLTextWriter and StringWriter; but adding to the new xml as node with this string is causing problems. The following :

 Private Sub btnXMLtoStr_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnXMLtoStr.Click

        Dim strXML As String

        Dim oXML As New XmlDocument()
        Dim oRoot As XmlNode
        Dim oNode As XmlNode
        Dim oDB As XmlNode
        Dim oTable As XmlNode

        oRoot = oXML.CreateElement("WindcatcherSettings")
        oNode = oXML.AppendChild(oRoot)

        oDB = oXML.CreateElement("ThresholdXML")
        strXML = GetXMLString("C:\Thresholds.xml")

        oNode.AppendChild(oDB)

        oTable = oXML.CreateElement(strXML)
        oDB.AppendChild(oTable)

        oXML.Save("C:\AllSettings.xml")
    End Sub


    Private Function GetXMLString(ByVal XMLPath As String) As String

        Dim xmlDoc As New XmlDocument()
        Dim sw As New StringWriter()
        Dim sVal As String


        Try
            xmlDoc.Load(XMLPath)
            Dim xw As New XmlTextWriter(sw)
            xmlDoc.WriteTo(xw)
            sVal = sw.ToString()
        Catch ex As Exception

        End Try

        GetXMLString = sVal
    End Function

Error is thrown on link         oTable = oXML.CreateElement(strXML)

After writing is it possible to retrieve all the node values as string? Say I want to search for "ThresholdXML" and get all the node values as String? Please help

Avatar of Roshan Davis
Roshan Davis
Flag of United States of America image

>> Error is thrown on link         oTable = oXML.CreateElement(strXML)
can you try this?
        'Create a document fragment.
        Dim docFrag As XmlDocumentFragment = doc.CreateDocumentFragment()

        'Set the contents of the document fragment.
        docFrag.InnerXml = strXML

        'Add the children of the document fragment to the
        'original document.
        oDB.AppendChild(docFrag)

Open in new window

Avatar of shanvidhya
shanvidhya

ASKER

Roshmon,

It throws Invalid Operation exception at oDB.AppendChild(docFrag)


Roshmon,Following is the xml file I am trying to use
SourceDoc.xml
ASKER CERTIFIED SOLUTION
Avatar of Roshan Davis
Roshan Davis
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The following line of code solve the problem:

sVal = xmlDoc.SelectSingleNode("iDENEventXML").InnerText;

Roshmon solution partly solved the problem. So 50% points can go to Roshmon

Thanks,
Shan