Click here to Skip to main content
15,885,881 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Sample XML:
HTML
<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:

C#
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);		


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

``node.RemoveAll()`` will not delete the node itself.
Try something like following-
C#
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 :)
 
Share this answer
 
Comments
SaravanaKumar@sk 25-Nov-15 23:33pm    
Ya it helps me, This code is correct, now i get the correct answer,

Thank You Very much
Suvendu Shekhar Giri 26-Nov-15 1:49am    
Glad to know it helped. Please mark as answer if it helped you so that others can take reference :)
SaravanaKumar@sk 26-Nov-15 2:10am    
okay.
You have to remove the node you want from its parent node.
C#
if (attribute.Value == TextId) {
   XmlNode parentNode = node.ParentNode;
   parentNode.RemoveChild(node);
}

Hope this helps.
 
Share this answer
 
Comments
SaravanaKumar@sk 25-Nov-15 23:32pm    
thanks for the post
 
Share this answer
 
Comments
SaravanaKumar@sk 25-Nov-15 23:31pm    
thanks for the link

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900