Click here to Skip to main content
15,887,427 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
How to remove "
XML
<enrichment-required>mf_compliant</enrichment-required>
" node from the below XML in c#?

XML
<?xml version="1.0" encoding="utf-8"?>
<request>
    <user-info>
        <app-id />
        <license-key />
        <site-id />
        <request-timestamp>2012-08-24T16:22:48</request-timestamp>
    </user-info>

    <enrichment-request>
        <dr-nbr>200700611609__8_13000</dr-nbr>
        <mf-target-edition>MF_2012</mf-target-edition>
        <enrich>
            <enrichment-required>mf_compliant</enrichment-required>
            <enrichment-required>mf-source-edition-group</enrichment-required>
            <enrichment-required>division</enrichment-required>
            <enrichment-required>section</enrichment-required>
            <enrichment-required>part</enrichment-required>
            <enrichment-required>products</enrichment-required>
            <enrichment-required>manufacturers</enrichment-required>
            <enrichment-required>brands</enrichment-required>
            <enrichment-required>trades</enrichment-required>


        </enrich>

        <enrich-details entity="division">
            <enrich-level key="boundary">yes</enrich-level>
            <enrich-level>source_id</enrich-level>
            <enrich-level>source_title</enrich-level>
            <enrich-level>normalized_id</enrich-level>
            <enrich-level>normalized_title</enrich-level>
        </enrich-details>
    </enrichment-request>
</request>
Posted
Comments
Member 9353131 11-Sep-12 5:14am    
In the same Xml file, how can I remove the entire block of "enrich-details entity="division"" node?
pradiprenushe 11-Sep-12 5:29am    
XmlNodeList xnList = doc.SelectNodes("request/enrichment-request/enrich-details");
foreach (XmlNode xn in xnList)
{
xn.RemoveChild(xn.FirstChild);

}
Member 9353131 11-Sep-12 5:51am    
how can I remove the node of enrich-details where innertext = "normalized_id"

Try this
C#
XmlDocument doc = new XmlDocument();
doc.Load(XmlfileName);
XmlNodeList xnList = doc.SelectNodes("request/enrichment-request/enrich/enrichment-required");
            foreach (XmlNode xn in xnList)
            {
                if (xn.FirstChild.Value == "mf_compliant")
                { 
                xn.RemoveChild(xn.FirstChild);
                }
            }
doc.Save(XmlfileName);
 
Share this answer
 
v3
Comments
Member 9353131 11-Sep-12 3:42am    
pradiprenushe, your solution removes the entire "enrich" node, but i just want to remove the node which contains "mf_compliant" as innertext.
Thanks in advance.
pradiprenushe 11-Sep-12 4:34am    
Try my updated answer
C#
// Input File Path.
XDocument doc = XDocument.Load("input.xml");

var q = from node in doc.Descendants("enrichment-required")
        where node != null && node.Value == "mf_compliant"
        select node;

q.ToList().ForEach(x => x.Remove());

// Output File Path.
doc.Save("output.xml");
 
Share this answer
 
v2
Comments
Member 9353131 11-Sep-12 3:43am    
sushil.mate, I couldn't work with the given solution as i get error at "doc.Descendants"
Sushil Mate 11-Sep-12 4:51am    
i have created console application where this code working properly. what exactly error you getting?
Here is a quick and dirty solution, i tested it briefly and that was the fastest I came up with. Probably there are some nicer solutions:
C#
XmlDocument document = new XmlDocument();
document.Load("C:\\Temp\\New.xml");
XmlNode nodeReq = document.SelectSingleNode("request");
if (nodeReq != null)
{
   XmlNode nodeEnReq = nodeReq.SelectSingleNode("enrichment-request");
   if (nodeEnReq != null)
   {
      XmlNode nodeEnrich = nodeEnReq.SelectSingleNode("enrich");
      if (nodeEnrich != null)
      {
         foreach (XmlNode node in nodeEnrich.ChildNodes)
         {
            if (node.InnerText == "mf_compliant")
               nodeEnrich.RemoveChild(node);
         }
      }
   }
}
document.Save("C:\\Temp\\New.xml");
 
Share this answer
 
Comments
Member 9353131 11-Sep-12 5:07am    
Hi, thanks its working good.
JF2015 11-Sep-12 5:14am    
So, if you don't mind, please upvote and accept my answer. Thanks!

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