我实现了一个包含以下数据的XML文档

<TrafficPattern>
    <WayPoint>
        <Radial>001</Radial>
        <Distance>0.36</Distance>
        <Latitude>
            <Degrees>48</Degrees>
            <Minutes>31.7363644</Minutes>
        </Latitude>
        <Longitude>
            <Degrees>11</Degrees>
            <Minutes>57.53425</Minutes>
        </Longitude>
     </WayPoint>
        <WayPoint>
        <Radial>090</Radial>
        <Distance>0.56</Distance>
        <Latitude>
            <Degrees>48</Degrees>
            <Minutes>31.7363644</Minutes>
        </Latitude>
        <Longitude>
            <Degrees>11</Degrees>
            <Minutes>57.53425</Minutes>
        </Longitude>
    </WayPoint>
    <WayPoint>
        <Radial>240</Radial>
        <Distance>0.56</Distance>
        <Latitude>
            <Degrees>48</Degrees>
            <Minutes>31.7363644</Minutes></Latitude>
        <Longitude>
            <Degrees>11</Degrees>
            <Minutes>57.53425</Minutes></Longitude>
    </WayPoint>
    <WayPoint>
        <Radial>346</Radial>
        <Distance>0.56</Distance>
        <Latitude>
            <Degrees>48</Degrees>
            <Minutes>31.7363644</Minutes></Latitude>
        <Longitude>
            <Degrees>11</Degrees>
            <Minutes>57.53425</Minutes></Longitude>
    </WayPoint>
</TrafficPattern>


我使用XMLDocument编写了上述XML文件。

现在,我想在单击按钮时从XML文件中删除航路点。

有人可以建议我如何使用XMLDocument删除它吗?

最佳答案

您需要使用XmlNode.RemoveChild方法,但是请注意,仅当将其应用于要删除的节点的父节点时,该方法才会起作用,否则将出现异常:


  要删除的节点不是该节点的子节点


removing nodes from an XmlDocument中所述。

以下RemoveNode方法基于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);
    }
}


上面的方法创建以下输出:


  原始流量模式:
  
  径向:001,距离:0.36,纬度:(48,31.7363644),经度:(11,57.53425)
  
  径向:090,距离:0.56,纬度:(48,31.7363644),经度:(11,57.53425)
  
  径向:240,距离:0.56,纬度:(48,31.7363644),经度:(11,57.53425)
  
  径向:346,距离:0.56,纬度:(48,31.7363644),经度:(11,57.53425)
  
  用radius = 090删除节点
  
  新的流量模式:
  
  径向:001,距离:0.36,纬度:(48,31.7363644),经度:(11,57.53425)
  
  径向:240,距离:0.56,纬度:(48,31.7363644),经度:(11,57.53425)
  
  径向:346,距离:0.56,纬度:(48,31.7363644),经度:(11,57.53425)


我使用的支持方法复制如下。

第一种方法创建了一个航路点,因此上面的代码并不那么混乱。为简单起见,所有内容均为string,但我可能会选择更好的参数类型

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


并且此方法显示XML文档:

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));
        }
    }
}

10-07 19:33