1

I have a XML file in my Visual Web Developer project that looks like this:

<?xml version="1.0" encoding="utf-8"?>
<complaints>
    <complaint>
        <user>omern</user>
        <content>asd</content>
        <ID>1</ID>
    </complaint>
    <complaint>
        <user>omeromern</user>
        <content>try2</content>
        <ID>2</ID>
    </complaint>
</complaints>    

I want to delete complaint nodes that have and ID of 2. How can I do this?

1
  • 2
    Mark it with your mouse, then press del key. Make sure to save the file to disk afterwards, otherwise the complaint will still be there the next time you open the file. P.S.: In case you don't appreciate the humorous aspect of my comment, consider that with the amount of information you gave in your question, this is a perfectly valid solution of your problem. Commented May 17, 2015 at 20:16

2 Answers 2

1

You can use the System.Xml.XmlDocument class to modify XML documents in C#. Note that this class lives in the System.Xml.dll assembly, so you will need to add a reference to System.Xml in your project.

using System.Xml;
internal class XmlExample
{
    /// <summary>
    /// Takes an XML string and removes complaint nodes with an ID of 2.
    /// </summary>
    /// <param name="xml">An XML document in string form.</param>
    /// <returns>The XML document with nodes removed.</returns>
    public static string StripComplaints(string xml)
    {
        XmlDocument xdoc = new XmlDocument();
        xdoc.LoadXml(xml);
        XmlNodeList nodes = xdoc.SelectNodes("/complaints/complaint[ID = '2']");
        XmlNode complaintsNode = xdoc.SelectSingleNode("/complaints");
        foreach (XmlNode n in nodes)
        {
            complaintsNode.RemoveChild(n);
        }

        return xdoc.OuterXml;
    }
}

Usage:

string xml = @"<?xml version=""1.0"" encoding=""utf-8""?>
                <complaints>
                    <complaint>
                        <user>omern</user>
                        <content>asd</content>
                        <ID>1</ID>
                    </complaint>
                    <complaint>
                        <user>omeromern</user>
                        <content>try2</content>
                        <ID>2</ID>
                    </complaint>
                </complaints>";
xml = XmlExample.StripComplaints(xml);
Sign up to request clarification or add additional context in comments.

Comments

1
//using System.Xml;

public string RemoveComplaintWhereIDis(string xml, string id)
{
    XmlDocument x = new XmlDocument();
    xml.LoadXml(xml);
    foreach (XmlNode xn in x.LastChild.ChildNodes)
    {
        if (xn.LastChild.InnerText == id)
        {
            x.LastChild.RemoveChild(xn);
        }
    }
    return x.OuterXml;
}

Basic Usage:

string x = @"<?xml version=""1.0"" encoding=""utf-8""?>
             <complaints>
                 <complaint>
                     <user>omern</user>
                     <content>asd</content>
                     <ID>1</ID>
                 </complaint>
                 <complaint>
                     <user>omeromern</user>
                     <content>try2</content>
                     <ID>2</ID>
                 </complaint>
             </complaints>";

string without2 = RemoveComplaintWhereIDis(x, "2");

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.