kumar
kumar

Reputation: 1127

How can I check whether a node exists in an XML file?

How can I check whether a node exists in an XML file, and also count the number of nodes?

I have one XML file for an example:

 <Employee>
  <Emp>
    <Name id="1">   A     </Name>
    <Name id="2">   C     </Name>
    <Name id="3">   D     </Name>
   </Emp>
  </Employee>

Upvotes: 2

Views: 48311

Answers (10)

Neha
Neha

Reputation: 21

XmlDocument _xmlDoc = new XmlDocument();

_xmlDoc.Load(Server.MapPath("~/XMLFile.xml"));

XmlNode _node = _xmlDoc.SelectSingleNode("Employee/Emp");

if (_node != null)

{

    XmlNodeList _nodeList = _node.SelectNodes("Name");

    Response.Write(_nodeList.Count);
}

else

{

    Response.Write("Emp node doesnot exist");

}

Upvotes: 2

Nayana Setty
Nayana Setty

Reputation: 1001

getElementsByTagName["tagname"] is also a DOM method which can be used to get a node. If the node does not match, method with return null.

Upvotes: 0

Stephen Friederichs
Stephen Friederichs

Reputation: 1059

I'm assuming you're using XSL to transform this document then I would assume that a variable would give the best functionality. You'd use this:

<xsl:variable name="Name_Count" select="count(//Name)"/>

This will give you the number of nodes of Name and you can change that to anything you'd like. Obviously if it's zero then there are none, otherwise it's the count.

Upvotes: 2

Manikandan
Manikandan

Reputation:

int nNodeExistCount = xmlOuput.GetElementsByTagName("NodeName").Count;

if (nNodeExistCount>0)
{
    Response.write(" The NodeName exists!");
}
else
{
    Response.write(" The NodeName does not exist!");
}

Upvotes: 0

Pramodh
Pramodh

Reputation: 1

If you are using XSLT transformation just trythis:

< xsl:choose>

< xsl:when test="//Employee/Emp">

< -- Node exists-->

< /xsl:when>

< xsl:otherwise>

< --Node does not exist-->

< /xsl:otherwise>

< /xsl:choose>

Upvotes: 0

rasx
rasx

Reputation: 5338

LINQ is great. But just in case you are stuck on a system with .NET 2.x you might have to do it the "old" (XPath) way (where xmlFragment is your string of XML above):

XPathDocument doc = new XPathDocument(new StringReader(xmlFragment));
XPathNavigator n = doc.CreateNavigator().SelectSingleNode("//Name[@id='4']");
if(n==null){//Node does not exist}

Upvotes: 1

jottos
jottos

Reputation: 20358

If you are programming in Java there are two related libraries you should look at.

JDOM - http://www.jdom.org/ DOM4J - http://www.dom4j.org/

I'd look at Dom4j 2.0 now since it's got support for generics, XPath, and now has some better high level support. Dom4j I think was forked from the earlier jdom.

In either you can read XML from a file, URL, string etc, parse it and check for nodes in only a few lines of code.

Upvotes: 1

eglasius
eglasius

Reputation: 36037

With linq 2 xml in c#:

var employee = XElement.Load(someStream);
var emp = employee.Element("Emp");
if( emp != null )
{
   int count = emp.Elements("Name").Count();
}

Upvotes: 6

Aaron
Aaron

Reputation: 829

As an alternative to XPath, many languages that have XML DOM support will allow you to call a method on an XML Document like:

GetAllNodesWithTagName(string tagname);

Your code to see if it exists would look something like this (written in pseudocode):

int num_nodes = 0;
string node_name = "Name"; // want to find all of the <Name> tags
XMLNode [] nodes = GetNodesWithTagName(node_name);
num_nodes = nodes.Length;

XPath is good, but it's better suited for easily navigating an XML document in interesting and complex ways. This code will be a bit more straightforward than the corresponding XPath code.

Upvotes: 0

Dan Breslau
Dan Breslau

Reputation: 11522

There are at least 4 nodes here, assuming that your </Emp> is matched by an opening <Emp> tag: <Emp>, <Name>, ID, and the string " D " would all be represented as nodes. It's not clear from your question whether you really would want to count all of these. I'm also not sure whether you want to determine the existence of a specific one of them.

Ultimately, though, XPath is probably what you're looking for.

Upvotes: 0

Related Questions