如何使用c#从XML中删除父标记 [英] How to remove parent tag from XML using c#

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

问题描述

<MainParents>
      <Parent>
        <Child 1>Something</Child 1>
        <Child 2>Something</Child 2>
        <Child 3>Something</Child 3>
        <Child 4>Something</Child 4>
      </Parent>
</MainParents>



------ ----------------------------

i只想删除Parent标记而不删除其内容标记在这个XML中你可以帮助我吗...

最后我需要输出像。 。 。


----------------------------------
i would like to remove only the "Parent" tag not its content tags in this XML can you any body help me ...
finally i need output like . . .

<MainParents>
    <Child 1>Something</Child 1>
    <Child 2>Something</Child 2>
    <Child 3>Something</Child 3>
    <Child 4>Something</Child 4>
 </MainParents>

推荐答案

你可以尝试像这样..



1)阅读< parent>使用内部xml标记和复制所有子节点

2)现在转到MainParents的根节点

3)现在用你的xml字符串替换内部xml字符串第一步阅读。



i认为它会解决你的问题。



但我不能承受站起来为什么你需要这个。您可以通过步骤父标记直接访问子节点。
you can try like this..

1) read the <parent> tag and copy all child node using inner xml
2) now go to the root node that is MainParents
3) now replace the inner xml string with your xml string which is read in first step.

i think it will solve your problem.

but i can't under stand why you need this. you can directly access the child node by steping on the Parent tag.


试试这个..



try this..

foreach (XmlNode chkNode in nodes)
{                
    string currentName = "Test 1";
    if (!nameDict.ContainsKey(currentName))
    {
        XmlNode parent = chkNode.ParentNode;
        parent.ParentNode.RemoveChild(parent);
    }
}


这篇关于如何使用c#从XML中删除父标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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