You need to use the XmlNode.RemoveChild
method, but notice that this will only work if you apply it to the parent of the node you're trying to remove, or you will get the exception:
The node to be removed is not a child of this node
as explained in removing nodes from an XmlDocument.
The following RemoveNode
method is based on a simple XML example.
public sealed class TestXmlNodeRemoval
{
public static void RemoveNode()
{
var xmlDocument = new XmlDocument();
var xmlTrafficPattern = xmlDocument.CreateElement("TrafficPattern");
xmlDocument.AppendChild(xmlTrafficPattern);
xmlTrafficPattern.AppendChild(CreateWayPoint(xmlDocument,
"001", "0.36", "48", "31.7363644", "11", "57.53425"));
xmlTrafficPattern.AppendChild(CreateWayPoint(xmlDocument,
"090", "0.56", "48", "31.7363644", "11", "57.53425"));
xmlTrafficPattern.AppendChild(CreateWayPoint(xmlDocument,
"240", "0.56", "48", "31.7363644", "11", "57.53425"));
xmlTrafficPattern.AppendChild(CreateWayPoint(xmlDocument,
"346", "0.56", "48", "31.7363644", "11", "57.53425"));
Console.WriteLine(@"Original traffic pattern:");
DisplayXmlDocument(xmlDocument);
// create an arbitrary criterion to remove an element
const string radialToRemove = @"090";
Console.WriteLine(@"Remove node with radial=" + radialToRemove);
if (xmlDocument.DocumentElement != null)
{
for (var i = 0; i < xmlDocument.DocumentElement.ChildNodes.Count; ++i)
{
var radial =
xmlDocument.DocumentElement.ChildNodes[i].SelectSingleNode("Radial");
if (radial == null || (radial.InnerText != radialToRemove))
{
continue;
}
var nodeToRemove = xmlDocument.DocumentElement.ChildNodes[i];
// note that you need to remove node from the Parent
if (nodeToRemove.ParentNode != null)
{
nodeToRemove.ParentNode.RemoveChild(nodeToRemove);
}
break;
}
}
Console.WriteLine(@"New traffic pattern:");
DisplayXmlDocument(xmlDocument);
}
}
This above method creates the following output:
Original traffic pattern:
Radial:001, Distance:0.36, Latitude:(48, 31.7363644), Longitude:(11, 57.53425)
Radial:090, Distance:0.56, Latitude:(48, 31.7363644), Longitude:(11, 57.53425)
Radial:240, Distance:0.56, Latitude:(48, 31.7363644), Longitude:(11, 57.53425)
Radial:346, Distance:0.56, Latitude:(48, 31.7363644), Longitude:(11, 57.53425)
Remove node with radial=090
New traffic pattern:
Radial:001, Distance:0.36, Latitude:(48, 31.7363644), Longitude:(11, 57.53425)
Radial:240, Distance:0.56, Latitude:(48, 31.7363644), Longitude:(11, 57.53425)
Radial:346, Distance:0.56, Latitude:(48, 31.7363644), Longitude:(11, 57.53425)
The supporting methods I used are copied below.
The first method creates a way point, so the code above isn't so cluttered. For simplicity, everything's a string
, but I probably would choose better parameter types
private static XmlElement CreateWayPoint(XmlDocument xmlDoc,
string radial,
string distance,
string latDegrees,
string latMinutes,
string longDegrees,
string longMinutes)
{
var xmlWayPoint = xmlDoc.CreateElement("WayPoint");
var xmlRadial = xmlDoc.CreateElement("Radial");
xmlRadial.InnerText = radial;
xmlWayPoint.AppendChild(xmlRadial);
var xmlDistance = xmlDoc.CreateElement("Distance");
xmlDistance.InnerText = distance;
xmlWayPoint.AppendChild(xmlDistance);
var xmlLatitude = xmlDoc.CreateElement("Latitude");
var xmlLatDegrees = xmlDoc.CreateElement("Degrees");
xmlLatDegrees.InnerText = latDegrees;
xmlLatitude.AppendChild(xmlLatDegrees);
var xmlLatMinutes = xmlDoc.CreateElement("Minutes");
xmlLatMinutes.InnerText = latMinutes;
xmlLatitude.AppendChild(xmlLatMinutes);
xmlWayPoint.AppendChild(xmlLatitude);
var xmlLongitude = xmlDoc.CreateElement("Longitude");
var xmlLongDegrees = xmlDoc.CreateElement("Degrees");
xmlLongDegrees.InnerText = longDegrees;
xmlLongitude.AppendChild(xmlLongDegrees);
var xmlLongMinutes = xmlDoc.CreateElement("Minutes");
xmlLongMinutes.InnerText = longMinutes;
xmlLongitude.AppendChild(xmlLongMinutes);
xmlWayPoint.AppendChild(xmlLongitude);
return xmlWayPoint;
}
And this method displays the XML document:
private static void DisplayXmlDocument(XmlNode xmlDoc)
{
var wayPoints = xmlDoc.SelectNodes("TrafficPattern/WayPoint");
if (wayPoints == null)
{
return;
}
foreach (XmlNode wayPoint in wayPoints)
{
var radial = wayPoint.SelectSingleNode("Radial");
var distance = wayPoint.SelectSingleNode("Distance");
var latitudeDegrees = wayPoint.SelectSingleNode("Latitude/Degrees");
var latitudeMinutes = wayPoint.SelectSingleNode("Latitude/Minutes");
var longitudeDegrees = wayPoint.SelectSingleNode("Longitude/Degrees");
var longitudeMinutes = wayPoint.SelectSingleNode("Longitude/Minutes");
if (radial != null &&
distance != null &&
latitudeDegrees != null &&
latitudeMinutes != null &&
longitudeDegrees != null &&
longitudeMinutes != null)
{
Console.WriteLine(string.Format("Radial:{0},
Distance:{1},
Latitude:({2}, {3}),
Longitude:({4}, {5})",
radial.InnerText,
distance.InnerText,
latitudeDegrees.InnerText,
latitudeMinutes.InnerText,
longitudeDegrees.InnerText,
longitudeMinutes.InnerText));
}
}
}