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

.NET ProgrammingC#Visual Basic.NET

Avatar of undefined
Last Comment
shanvidhya

8/22/2022 - Mon
Roshan Davis

Roshan Davis

Roshan Davis

>> 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

Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
shanvidhya

ASKER
Roshmon,

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


shanvidhya

ASKER
Roshmon,Following is the xml file I am trying to use
SourceDoc.xml
ASKER CERTIFIED SOLUTION
Roshan Davis

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
shanvidhya

ASKER
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
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.