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:
!--removed tag-->
!--removed tag-->
!--removed tag-->
!--removed tag-->
!--removed tag-->
!--removed tag-->
What I want to do is clone all the nodes and included data and copy them to the new file in between the tags.!--removed tag-->!--removed tag-->
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);
!--removed tag-->
No one has replied yet! Why not be the first?
Sign in or Join us (it's free).