I have an xmlfile
I want to write code using System.XML namespace to delete node name="abc".
Please help.. I will highly appreciate your help...
Thanks
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Peter PetrovPosted Jun 5, 2006, 10:27 AM
1) Your XML input file is not quite ok
www.abc.com" enable="Yes" />
www.abc1.com" enable="Yes" />
Use this:
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.
asdklj kljasdPosted Jun 4, 2006, 1:34 PM
"Object reference not set to an instance of an object."
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 + "']");}