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
URGENT:::XML Question
2 Answers
Know the answer? Post it. Somebody with the same question will find it here.
asdklj kljasd3 pointsPosted Jun 4, 2006, 9:34 AM EDT
"Object reference not set to an instance of an object."
node.RemoveChild(node.SelectSingleNode("/website[@name='" + this.cboURLName.Text + "']"));Console.WriteLine("/data/website[name='" + this.cboURLName.Text + "']");
}
Peter Petrov6 pointsPosted Jun 5, 2006, 6:27 AM EDT
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:
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.