Cloning xml nodes into a new xmldoc

xml , asp.net , csharp London, United Kingdom
  • 7 years ago

    Hi,

    I am trying to clone a set of nodes from one xmldocument to another as below:

    my source document looks like this....

    <gpx
      version="1.0"
      creator="Someone"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xmlns="http://www.topografix.com/GPX/1/0"
      xsi:schemaLocation="http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd">
        <trk>
            <name></name>
            <time></time>
            <trkseg>
                <trkpt lat="" lon="">
                    <ele></ele>
                    <time></time>
                </trkpt>
            </trkseg>
            <trkseg>
                <trkpt lat="" lon="">
                    <ele></ele>
                    <time></time>
                </trkpt>
            </trkseg>
            <trkseg>
                <trkpt lat="" lon="">
                    <ele></ele>
                    <time></time>
                </trkpt>
            </trkseg>
        </trk>
    </gpx>
    

    I have a destination document template created which will have a load more stuff in but a similar structure as follows:

    What I want to do is clone all the nodes and included data and copy them to the new file in between the tags.

    However, somethings not right with my c# code as follows as I only get null back when the xpath parses. If I do * then that works but I don't want everything, I only want the trkseg's back. I'd have assumed that the xpath below would have worked fine.

            gpxDoc.Load(MapPath("~/datafiles/" + idOfEntry.ToString() + ".xml"));
    
            //Copy the trkseg nodes and data from the original to the new file
    
            XmlNode objToBeCloned;
            XmlElement root = gpxDoc.DocumentElement; // original file
    
    
            //Create and instance of the new file
            XmlDocument newGpxDoc = new XmlDocument();
    
            XmlElement root2 = newGpxDoc.DocumentElement; //new file
    
            //This document is a template which doesnt have any <trkseg> nodes
    
            newGpxDoc.Load(MapPath("~/defaultGCDataFile.xml"));
    
            //This should have all the trkseg's in from the original file
            objToBeCloned = root.SelectSingleNode(@"/trk/trkseg"); 
            XmlNode objNewNode = objToBeCloned.CloneNode(true);
    
            //Insert the nodes after the trk tag in the new file
    
            root2.InsertAfter(objNewNode, newGpxDoc.LastChild);
    

Post a reply

No one has replied yet! Why not be the first?

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.

“Owning a computer without programming is like having a kitchen and using only the microwave oven” - Charles Petzold