如何删除XML节点?

时间:2014-01-10 14:36:50

标签: c# asp.net xml

我已经创建了从XML中选择和删除节点的方法。我的代码运行但不删除XML节点。

我不知道代码出错的地方请帮我解决这个问题:

public XmlNodeList getXmlNodeList(string XmlNodeName)
{
    XmlDocument doc = new XmlDocument();
    doc.Load(_connection);
    XmlNodeList elemList = doc.GetElementsByTagName(XmlNodeName);
    return elemList;
}

public void deleteXmlNode(string XmlNodeName, string XmlNodeValue)
{
    XmlDocument xdoc = new XmlDocument();
    xdoc.Load(_connection);
    XmlNodeList xnode = getXmlNodeList(XmlNodeName);

    foreach (XmlNode item in xnode)
    {
        if (item.InnerText == XmlNodeValue)
        {
            item.ParentNode.RemoveChild(item);
            break;
        }
    }
    xdoc.Save(_connection);
}

我的XML代码:

<AriaGostarInformation>
  <MenuInformation>
    <MenuNames>
      one
    </MenuNames>
    <MenuNames Name="1">two</MenuNames>
    <MenuNames Name="2">three</MenuNames>
    <MenuNames Name="3">four</MenuNames>
    <MenuNames Name="4">five</MenuNames>
    <MenuNames Name="5">six</MenuNames>
    <SubMenuNames parentName="1">
      fgfgfgfgs
    </SubMenuNames>
    <SubMenuNames parentName="3">
    </SubMenuNames>
  </MenuInformation>
  <SliderInformation>
  </SliderInformation>
  <LasProductInformation>
    <Product>
      <ImageAddress alt="">../Static/Css/Images/c.jpg</ImageAddress>
      <Subject>bere</Subject>
      <Description>nare</Description>
    </Product>
    <Product>
      <ImageAddress alt="">../Static/Css/Images/c.jpg</ImageAddress>
      <Subject>bere</Subject>
      <Description>nare</Description>
    </Product>
    <Product>
      <ImageAddress alt="">../Static/Css/Images/c.jpg</ImageAddress>
      <Subject>bere</Subject>
      <Description>nare</Description>
    </Product>
    <Product>
      <ImageAddress alt="">../Static/Css/Images/c.jpg</ImageAddress>
      <Subject>bere</Subject>
      <Description>nare</Description>
    </Product>
    <Product>
      <ImageAddress alt="">../Static/Css/Images/c.jpg</ImageAddress>
      <Subject>bere</Subject>
      <Description>nare</Description>
    </Product>
    <Product>
      <ImageAddress alt="">../Static/Css/Images/c.jpg</ImageAddress>
      <Subject>bere</Subject>
      <Description>nare</Description>
    </Product>
    <Product>
      <ImageAddress alt="">../Static/Css/Images/c.jpg</ImageAddress>
      <Subject>bere</Subject>
      <Description>nare</Description>
    </Product>
    <Product>
      <ImageAddress alt="">../Static/Css/Images/c.jpg</ImageAddress>
      <Subject>bere</Subject>
      <Description>nare</Description>
    </Product>
    <Product>
      <ImageAddress alt="">../Static/Css/Images/c.jpg</ImageAddress>
      <Subject>bere</Subject>
      <Description>nare</Description>
    </Product>
    <Product>
      <ImageAddress alt="">../Static/Css/Images/c.jpg</ImageAddress>
      <Subject>bere</Subject>
      <Description>nare</Description>
    </Product>
    <Product>
      <ImageAddress alt="">../Static/Css/Images/c.jpg</ImageAddress>
      <Subject>bere</Subject>
      <Description>nare</Description>
    </Product>
    <Product>
      <ImageAddress alt="">../Static/Css/Images/c.jpg</ImageAddress>
      <Subject>bere</Subject>
      <Description>nare</Description>
    </Product>
    <Product>
      <ImageAddress alt="">../Static/Css/Images/c.jpg</ImageAddress>
      <Subject>bere</Subject>
      <Description>nare</Description>
    </Product>
    <Product>
      <ImageAddress alt="">../Static/Css/Images/c.jpg</ImageAddress>
      <Subject>bere</Subject>
      <Description>nare</Description>
    </Product>
    <Product>
      <ImageAddress alt="">../Static/Css/Images/c.jpg</ImageAddress>
      <Subject>bere</Subject>
      <Description>nare</Description>
    </Product>
  </LasProductInformation>
</AriaGostarInformation>

1 个答案:

答案 0 :(得分:0)

通过加载两个单独的XmlDocument对象来查找xmltree的节点,会损害您的性能。但是,看起来你没有任何错误。您可能没有传递有效参数。您是否尝试添加任何跟踪以查看提交给方法的值是什么?还建议使用以下代码为删除节点方法使用Linq-to-Xml将这两种方法组合成一种非常短的方法。以下内容可以删除所有匹配的节点:

public void deleteXmlNode(string xmlNodeName, string xmlNodeValue)
{
     XDocument xDoc = XDocument.Load(_connection);
     var xNodeList = xDoc.Descendants(xmlNodeName).Where(n => n.Value == xmlNodeValue);
     xNodeList.Remove();
}

消息来源:http://msdn.microsoft.com/en-us/library/bb357554(v=vs.110).aspx