使用c#中的xmldocument从父节点中删除特定节点,但特定节点除外 [英] Remove specific child node from parent node except specific node using xmldocument in c#

查看:231
本文介绍了使用c#中的xmldocument从父节点中删除特定节点,但特定节点除外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

输入:

<产品>

  <产品主题 - 标题>

    < subject> BIOLOGY ARTICLE< / subject>

  < / Product-subject-title>

  < category>

    < code-id type =" pub"> e00005< / code-id>

    < title> test28< / title>

    < ranking> 10< / ranking>

  < / category>

  < category>

    < code-id type =" pub"> e00002< / code-id>

    < title> test34< / title>

    < ranking> 160< / ranking>

  < / category>

<Product>
  <Product-subject-title>
    <subject>BIOLOGY ARTICLE</subject>
  </Product-subject-title>
  <category>
    <code-id type="pub">e00005</code-id>
    <title>test28</title>
    <ranking>10</ranking>
  </category>
  <category>
    <code-id type="pub">e00002</code-id>
    <title>test34</title>
    <ranking>160</ranking>
  </category>

  < category>

    < code-id type =" pub"> e00004< / code-id>

    < title> test34< / title>

    < ranking> 160< / ranking>

  < / category>

  <category>
    <code-id type="pub">e00004</code-id>
    <title>test34</title>
    <ranking>160</ranking>
  </category>

  < category>

    < code-id type =" pub"> e00007< / code-id>

    < title> test34< / title>

    < ranking> 160< / ranking>

  < / category>< / Product>

  <category>
    <code-id type="pub">e00007</code-id>
    <title>test34</title>
    <ranking>160</ranking>
  </category></Product>

输出:

<产品>

  <产品主题 - 标题>

    < subject> BIOLOGY ARTICLE< / subject>

  < / Product-subject-title>

  < category>

    < code-id type =" pub"> e00002< / code-id>

    < title> test34< / title>

    < ranking> 160< / ranking>

  < / category>

< / Product>

<Product>
  <Product-subject-title>
    <subject>BIOLOGY ARTICLE</subject>
  </Product-subject-title>
  <category>
    <code-id type="pub">e00002</code-id>
    <title>test34</title>
    <ranking>160</ranking>
  </category>
</Product>

在这里,我想从父项中删除所有子项,这意味着从产品父节点中删除所有类别子节点特定类别子节点。

Here i want to remove all child from parent it means remove all category child node from product parent node except specific category child node.

XmlDocument doc = new XmlDocument();

XmlDocument doc = new XmlDocument();

doc.LoadXml(strxmlstring);

XmlNodeList childNode = doc.SelectNodes(" // category");

doc.LoadXml(strxmlstring);
XmlNodeList childNode = doc.SelectNodes("//category");

foreach(childNode中的XmlNode节点)

       ;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP; {

&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP ;&NBSP;&NBSP; node.ParentNode.RemoveChild(node);

foreach (XmlNode node in childNode)
                    {
                        node.ParentNode.RemoveChild(node);

除(< category>< code-id type =" pub"> e00002< / code-id>< title> test34< / title>< ranking> 160< / ranking>< / category>"); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;

except( "<category><code-id type="pub">e00002</code-id><title>test34</title><ranking>160</ranking></category>") ;                 

}

推荐答案

试试这个:

XmlNode product = doc.SelectSingleNode( "/Product" );
foreach( XmlNode category in product.SelectNodes( "category[code-id!='e00002']" ) )
{
   product.RemoveChild( category );
}


这篇关于使用c#中的xmldocument从父节点中删除特定节点,但特定节点除外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆