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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How Do I Find A Specific XML Node

Status
Not open for further replies.

SGLong

Programmer
Jun 6, 2000
405
US
I'm writing a program that needs to be able to find a specific node in an XML file and delete that node. Being new to C# I'm having trouble accomplishing this. Given the following file, how would I go about locating the node for "br_factors" and deleting it.

Code:
<tables>
   <table name="13d_control"/>
   <table name="13d_exclusions"/>
   <table name="13f_exclusions"/>
   <table name="apd"/>
   <table name="apdh"/>
   <table name="ar"/>
   <table name="ba"/>
   <table name="bn"/>
   <table name="br_factors"/>
   <table name="br_rates"/>
   <table name="br_tickets"/>
   <table name="buypwr"/>
   <table name="buypwr_calc"/>
   <table name="ca"/>
   <table name="cd"/>
</tables>

Steve
 
will give you the basics of XML (amoung other technologies). You will want to check out XPath. that's how you query xml files.

in .Net you'll use the System.XML namespace to access all the objects related to XML.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Try something like this:

XmlDocument doc;
doc.load("your xml file")

XmlNode node = doc.SelectSingleNode("//table[@name='13d_control']")
 
Matt,

This looks promising... Based on your suggestion here's the code that I'm using:
Code:
strCurrentNode="//table[@name='" + strTableName+"']";
XmlNode  myNode = l_XmlDoc.SelectSingleNode(strCurrentNode);
l_XmlDoc.RemoveChild(myNode);

which gives me this error:

Error:The node to be removed is not a child of this node. Occurred at System.Xml.XmlNode.RemoveChild(XmlNode oldChild)

What am I missing?

Steve
 
Steve,
Try and grab the root node like this:
XmlNode root = xmlDoc.DocumentElement
And then:
strCurrentNode="//table[@name='" + strTableName+"']";
XmlNode myNode = root.SelectSingleNode(strCurrentNode);
root.RemoveChild(myNode);
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top