从xmlNodeList中删除节点

本文关键字:节点 删除 xmlNodeList | 更新日期: 2023-09-27 18:15:42

这是我的代码

    public bool limitOutput()
    {
        double lowerLeftPointX = m_mapControl.CurrentExtent.LowerLeftPoint.X;
        double lowerLeftPointY = m_mapControl.CurrentExtent.LowerLeftPoint.Y;
        double upperRightPointX = m_mapControl.CurrentExtent.UpperRightPoint.X;
        double upperRightPointY = m_mapControl.CurrentExtent.UpperRightPoint.Y;
        for(int i = locationElements.Count - 1; i >= 0; i--)
        {
            if (Double.Parse(locationElements[i]["GEOMETRY_X"].InnerText) < lowerLeftPointX || Double.Parse(locationElements[i]["GEOMETRY_X"].InnerText) > upperRightPointX || Double.Parse(locationElements[i]["GEOMETRY_Y"].InnerText) < lowerLeftPointY || Double.Parse(locationElements[i]["GEOMETRY_Y"].InnerText) > upperRightPointY)
            {
                locationElements[i].ParentNode.RemoveChild(locationElements[i]);
            }
        }
        if (locationElements.Count == 0)
        {
            PearMessageBox.Show(PearMessageBox.mBoxType.simpleNotification, "No results found in specified area");
            return false;
        }
        return true;

    }

我正在尝试删除所有不在我设置的边界内的节点。删除行被执行,但实际上并没有删除,因为当我计算locationElements时,它仍然是执行方法之前的相同值。

你知道代码有什么问题吗

从xmlNodeList中删除节点

问题是由于RemoveChild()从源XmlDocument中删除了元素,但没有从预填充的XmlNodeList中删除元素。因此,您需要再次执行用于预填充locationElements变量的代码,如下所示:

//assume that GetLocationElements() is a method...
//...containing the same logic you used to populate locationElements variable
var updatedLocationElements = GetLocationElements();
if (updatedLocationElements.Count == 0)
{
    PearMessageBox.Show(PearMessageBox.mBoxType.simpleNotification, "No results found in specified area");
    return false;
}
return true;