Groups | Blog | Home
all groups > dotnet xml > october 2005 >

dotnet xml : Replacing a node



Martin Honnen
10/12/2005 12:00:00 AM


[quoted text, click to view]

node.ParentNode.ReplaceChild(newNode, node)
will replace node with newNode.

If you still have problems then provide more details on what you have tried.

--

Martin Honnen --- MVP XML
Ed A
10/12/2005 9:17:02 AM
Hi all:

Is there a way to replace a node and it's children? I tried using the
ReplaceChild method but that seems to only replace the first child only!!

Thanks
Ed A
10/12/2005 10:50:04 AM

This is what I've tried:

My XML file looks like this
......
<Collection name="DbTestCollection1" TopLeftX="-86.4037416919"
TopLeftY="20.63144694" BottomRightX="-86.3945403259"
BottomRightY="20.626370603" DefaultFloor="1">
<Layers PATH="C:\Program Files\Maps">
<Layer IMAGE="DB_map.tif" BASE="true" />
<Layer IMAGE="FloorPlan2.tif" BASE="false" BUILDING="1" FLOOR="1" />
</Layers>
<Buildings>
<Building number="1" CN="Building_1" />
</Buildings>
<Floors>
<Floor number="1" CN="Floor_1" BuildingNumber="1" />
</Floors>
<Columns>
<Column number="0" CN="Column A" BuildingNumber="1" />
</Columns>
</Collection>
......


I look for my local node in the file first then I create a new one from the
databse to replace it:

// Look for the node name in the collections file
XmlNode localNode = FindNode(_name,xmlDoc);

// now I create the new node
// Collection name
collectionNode = _xmlDoc.CreateNode(XmlNodeType.Element,"Collection","");

// Collection Name
XmlAttribute attr = _xmlDoc.CreateAttribute("name");
attr.Value = map.Name;
collectionNode.Attributes.Append(attr);
.....
....
....

// Then I call replace node
localNode.ParentNode.ReplaceChild(collectionNode,localNode);

this seems to only replace the
<Collection name=........></Collection>
tag and non of its children.


Thanks
Ed;;

[quoted text, click to view]
Pascal Schmitt
10/12/2005 9:33:49 PM
Hello!

[quoted text, click to view]

This looks as if you would be using two different XmlDocument-s xmlDoc
and _xmlDoc. This doesn't work.
To use "foreign" nodes from an other XmlDocument, you first have to
import them using XmlDocument.ImportNode:
<http://msdn2.microsoft.com/en-us/library/System.Xml.XmlDocument.ImportNode>

So, add the line

xmlDoc.ImportNode( collectionNode, /*Deep*/true );


--
AddThis Social Bookmark Button