Skip to content

URGENT:::XML Question

asdklj kljasd

Asked Jun 2, 2006, 4:15 PM EDT2 answers
Share
LinkedInX
Hi Guys,

I have an xmlfile
<data>
<website name="abc" url="www.abc.com" enable="Yes">
<website name="abc1" url="www.abc1.com" enable="Yes">
</data>

I want to write code using System.XML namespace to delete node name="abc".
Please help.. I will highly appreciate your help...

Thanks

2 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
     I am getting an error..

    "Object reference not set to an instance of an object."

    Code
    XmlDataDocument xmlDoc = new XmlDataDocument();
    xmlDoc.Load(dataFile);
    foreach(XmlNode node in xmlDoc.GetElementsByTagName("website"))
    {

    node.RemoveChild(node.SelectSingleNode("/website[@name='" + this.cboURLName.Text + "']"));

    Console.WriteLine("/data/website[name='" + this.cboURLName.Text + "']");

    }

    0
  • 0

    1) Your XML input file is not quite ok

    Use this:

    <data>
    <website name="abc" url="www.abc.com" enable="Yes" />
    <website name="abc1" url="www.abc1.com" enable="Yes" />
    </data>


    2) Your C# code has some bugs.
    When calling SelectSingleNode for example you shouldn't start the XPath
    string with / (forward slash) ... Remove that slash.
    Just start with the name of the sub-node. 

    You can compare your code with this code:

    C#
    using System;
    using System.Xml;
    using System.IO;
    namespace TestXML
    {
    class MainProg
    {
    [STAThread]
    static void Main(string[] args)
    {
    XmlDataDocument xmlDoc = new XmlDataDocument();
    
    string dataFile = "test_input.xml";
    string cboURLName = "abc";
    xmlDoc.Load(dataFile);
    
    XmlNodeList list = xmlDoc.GetElementsByTagName("data");
    foreach(XmlNode node in list)
    {
    Console.WriteLine("/data/website[name='" + cboURLName + "']");
    node.RemoveChild(node.SelectSingleNode("website[@name='" + cboURLName + "']"));
    }
    xmlDoc.Save(new System.IO.FileStream( "test_output.xml", FileMode.Append, FileAccess.Write) );
    }
    }
    }


    3) Make sure you have the test_input.xml file in the folder where your EXE is before you run the EXE. 
     
    Then check the output file test_output.xml which will be created by the program when you start it.

    0