delete node from XML

Category: c# language

Question

Hossein Assefi on Sun, 30 Sep 2012 13:14:54


Hi

i have a problem with the code i wrote in my program for removing a node from XML file.

this is the XML file I have :

  <?xml version="1.0" encoding="utf-16" ?> 
- <Membership_sections>
  <DAY>7</DAY> 
- <Membership_sections>
  <DAY>10</DAY> 
  </Membership_sections>
- <Membership_sections>
  <DAY>15</DAY> 
  </Membership_sections>
  </Membership_sections

and this is my code :

XmlDocument d = new XmlDocument();
d.Load(filename);
XmlNode n = d.SelectSingleNode("/Membership_sections[@DAY='" + textBox1.Text + "']");
n.ParentNode.RemoveChild(n);
MessageBox.Show("done");
the problem is that i get the below error. please help me solve this problem.
Object reference not set to an instance of an object.

Replies

Ahmed Ibrahim - MSFT on Sun, 30 Sep 2012 13:48:39


Please check-out below code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;

namespace ConsoleMessageBox
{
    class Program
    {
        static void Main(string[] args)
        {
            if (RemoveNode("10"))
            {
                Console.WriteLine("Node was deleted successfully");
            }
            else
            {
                Console.WriteLine("Failed to delete node");
            }
        }
        public static bool RemoveNode(string NodeValue)
        {
            string xml = @"<?xml version=""1.0"" encoding=""utf-16"" ?> 
 <Membership_sections>
  <DAY>7</DAY> 
 <Membership_sections>
  <DAY>10</DAY> 
  </Membership_sections>
 <Membership_sections>
  <DAY>15</DAY> 
  </Membership_sections>
  </Membership_sections>";
            XmlDocument d = new XmlDocument();
            d.LoadXml(xml);
            XmlNode selectedNode = d.SelectSingleNode(

                string.Format(System.Globalization.CultureInfo.InvariantCulture, "/Membership_sections/Membership_sections/DAY[text()='{0}']", NodeValue));

            if (selectedNode != null)
            {
                selectedNode.ParentNode.RemoveChild(selectedNode);
                return true;
            }

            return false;

        }
    }
}

Mitja Bonca on Sun, 30 Sep 2012 19:49:58


Add  '"+textBox1.Text+'" in between (single quotes and double quotes on both sides.

---

You can do it like:

xdoc.XPathSelectElement("Membership_sections[@DAY = '" + textBox1.Text + "']").Remove();

by using Xml.XPath namespace.


Mitja


Hossein Assefi on Tue, 02 Oct 2012 09:50:04


thanks for relying.

i did as you told me and i got a new problem with the line that loads the XML file. so i replaced it with load() and it works fine and the error is gone.

But, i don't know why but it doesn't save the changes. I traced the program line by line and it works perfect but i don't get it that the changes are not submitted.

can you tell me what the problem is ??  

Hossein Assefi on Tue, 02 Oct 2012 10:20:20


can you explain more ... thanks

Joon84 on Tue, 02 Oct 2012 13:12:21


i think the xml structure is little bit incorrect, please find the xml file that I used below and the code to remove the desired value (I used value 7 in place of textBox1.Text)

Check you need to save the file after removing the element from it. I saved the XML to "C:\a.xml". Note i have used UTF-8, not UTF-16 due to unicode conversion issue. If you wish to use UTF.16 maybe you can read the file and then perform LoadXML or so.

XmlDocument doc = new XmlDocument();
doc.Load(@"C:\a.xml");
XmlNodeList nodes = doc.SelectNodes("descendant::Membership_sections[DAY='7']");// in place of textBox1.Text

for (int i = nodes.Count - 1; i >= 0; i--)
{
    nodes[i].ParentNode.RemoveChild(nodes[i]);
}
doc.Save(@"C:\a.xml");

XML structure that i used:

<?xml version="1.0" encoding="UTF-8"?>
<Membership>
  <Membership_sections>
    <DAY>7</DAY>
  </Membership_sections>
  <Membership_sections>
    <DAY>10</DAY>
  </Membership_sections>
  <Membership_sections>
    <DAY>15</DAY>
  </Membership_sections>
</Membership>

regards

joon





Hossein Assefi on Wed, 03 Oct 2012 04:59:55


thank you 

it really helped.