如何删除xml中的根节点 [英] How to delete root node in xml

查看:305
本文介绍了如何删除xml中的根节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

示例XML:

Sample XML:

<Announcements>
  <Announce ID="118">
    <ViewType>1</ViewType>
    <Title>2nd record</Title>
    <PDate>11-25-2015</PDate>
    <AnnGroup>
    </AnnGroup>
    <Active>1</Active>
    <disorder>
    </disorder>
    <Description>System.Xml.XmlCDataSection</Description>
  </Announce>
  <Announce ID="117">
    <ViewType>1</ViewType>
    <Title>1st record</Title>
    <PDate>11-25-2015</PDate>
    <AnnGroup />
    <Active>0</Active>
    <IsBefore />
    <disorder />
    <Description><![CDATA[<p>1st record</p>]]></Description>
  </Announce>
</Announcements>







示例代码:






Sample Code:

XmlDocument doc = new XmlDocument();
                doc.Load(path);
                XmlNodeList nodes = doc.GetElementsByTagName("Announce");

                foreach (XmlNode node in nodes)
                {
                    foreach (XmlAttribute attribute in node.Attributes)
                    {
                        if (attribute.Value == TextId)
						{
						 node.RemoveAll();
						    UploadToSFTP.UploadFileToSFTPServer();
                            break;
						}
					}
				}
 doc.Save(path);		





工作但我想删除< announce>标签也,请帮帮我,

谢谢预付

Saravanakumar B



Its Working But i want remove <announce> tag also, please help me,
Thanks Advance
Saravanakumar B

推荐答案

``node.RemoveAll( )``不会删除节点本身。

尝试下面这样的事情 -

``node.RemoveAll()`` will not delete the node itself.
Try something like following-
XmlDocument doc = new XmlDocument();
doc.Load(path);
XmlElement el = (XmlElement)doc.SelectSingleNode("/Announcements/Announce[ID=TextId]");		
if(el != null) 
{ 
  el.ParentNode.RemoveChild(el); 
}
UploadToSFTP.UploadFileToSFTPServer();//not sure what it does for you
doc.Save(path);		





希望,它有帮助:)



Hope, it helps :)


你必须从中删除你想要的节点父节点。

You have to remove the node you want from its parent node.
if (attribute.Value == TextId) {
   XmlNode parentNode = node.ParentNode;
   parentNode.RemoveChild(node);
}



希望这会有所帮助。


Hope this helps.


您好,

请查看以下网址< br $> b $ b

http://www.c-sharpcorner.com/Blogs/10524/delete-xml-node-using-C-Sharp.aspx [ ^ ]





http:// stackoverflow。 com / questions / 17503619 / how-to-remove-root-element-in-c [ ^ ]
Hi,
Please check below url

http://www.c-sharpcorner.com/Blogs/10524/delete-xml-node-using-C-Sharp.aspx[^]


http://stackoverflow.com/questions/17503619/how-to-remove-root-element-in-c[^]


这篇关于如何删除xml中的根节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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