Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Deleting nodes from XML file in C#

Status
Not open for further replies.

markknowsley

Programmer
Aug 30, 2005
152
GB
Using the following XML file as an example:

Code:
<?xml version='1.0'?>
<Collection>
   <Book Id='1'>
      <Title>Principle of Relativity</Title>
      <Author>Albert Einstein</Author>
      <Genre>Physics</Genre>
   </Book>
   <Book Id='2'>
      <Title>Cosmos</Title>
      <Author>Carl Sagan</Author>
      <Genre>Cosmology</Genre>
   </Book>
</Collection>

I want to write a C# method that will delete the Book node with the Id='2'.

I have the following code:
Code:
XmlDocument doc = new XmlDocument();
                doc.Load("test.xml");

                XmlNodeList nodeList = doc.SelectNodes("//Collection");
                foreach (XmlNode node in nodeList)
                {
                    node.RemoveChild(node.SelectSingleNode("Book"));
                }

                doc.PreserveWhitespace = true;
                XmlTextWriter writer = new XmlTextWriter("test.xml", Encoding.Unicode);
                doc.WriteTo(writer);
                writer.Close();

which removes the first node with the id 'Book'. Please can someone tell me how i can modify this to delete the node where Book Id='2'?
 
instead of cycling through each node look into xPath queries. this is the filter language for xml.
Code:
//define variables
string fileName = "test.xml";
int bookId = 2;
string xPathQuery = string.format("//Book[ID={0}]", bookId);

//open document
XmlDocument doc = new XmlDocument();
doc.Load(fileName);

//remove node
XmlNode nodeToDelete = doc.SelectSingleNode(xPathQuery);
doc.RemoveChild(nodetoDelete);

//save
doc.PreserveWhitespace = true;
doc.Save(fileName);

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Instead of using:
node.RemoveChild(node.SelectSingleNode("Book"));
Try:
node.RemoveChild(node.SelectSingleNode("//Book[@Id='2']"));
 
Next question: I have some XML in this format

Code:
<employee ID="000001" Initials="G" Surname="RUSEDSKI                           ">
		<summary batchnumber="00841" batchitem="01" claimmonth="1" claimyear="2007" 		

Miles="154" amount="27.72" AuthStatus="Rejected" vmodifiedby="" vnotes=""/>
	</employee>
	<employee ID="000014" Initials="T" Surname="HENMAN                           ">
		<summary batchnumber="00841" batchitem="02" claimmonth="1" claimyear="2007" 		

Miles="24" amount="10.18" AuthStatus="Rejected" vmodifiedby="" vnotes=""/>
	</employee>

I've loaded the XML, created an XMLNodeList and then iterated through;
Code:
foreach (XmlNode node in nodeList)
{
node.RemoveChild(node.SelectSingleNode("//summary[@batchnumber='00841']);
}

This works fine and gets rid of the two nodes. However, I want to delete XML based on the value of the AuthStatus field - if this is set to 'Approved' then I want to delete the node.

But if I change the XPath to @authstatus='Approved' I get an 'Object Reference not set to an instance of an Object' error. Can someone tell me why this is?

Mark.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top