Home > Articles > LINQ Articles > How to check child node exists in XML using LINQ?

How to check child node exists in XML using LINQ?

by Thamilselvan J   on Jun 11, 2013   Category: LINQ  | Level: Beginner  |  Views: 2696    |  Points: 100   
Like this article? Bookmark and Share:
In this article I will show you, how to verify the child elements/nodes are exists in the XML while selecting values from the XML using LINQ.




Introduction


Generally we will use linq very often in xml operations such as create xml, modify and retrieve the values from xml for specific task. Sometime you will not get all child nodes under parent node. Here I will show you how to verify if child node exists in xml and get values from.

First create the xml using linq and see how to retrieve the values from xml with simple code snippets.

CreateXML

Using xDocument, we will create the sample employee details xml as shown below and load the xml values into gridview with conditions

XDocument xdoc = new XDocument

(new XDeclaration("1.0", "UTF-16", "true"),

new XElement("Company",

new XElement("Employee",

new XElement("ID", "100"),

new XElement("Name", "Kenny"),

new XElement("Job", "Manager"),

new XElement("Tel", "45639877"),

new XElement("Place", "UK"),

new XElement("zip", "21321")),  

new XElement("Employee",

new XElement("ID", "101"),

//new XElement("Name", "Jhon"),

new XElement("Job", "Sr Manager"),

new XElement("Tel", "45639878"),

new XElement("Place", "India"),

new XElement("zip", "456321")),

new XElement("Employee",

new XElement("ID", "102"),

new XElement("Name", "Mick"),

new XElement("Job", "Sr Manager"),

new XElement("Tel", "45639878"),

new XElement("Place", "India"),

new XElement("zip", "456321")),

new XElement("Employee",

new XElement("ID", "103"),

new XElement("Name", "Peter"),

new XElement("Job", "Manager"),

//new XElement("Tel", "3534533"),

new XElement("Place", "India"),

new XElement("zip", "456321"))

));


The above xml have list of employee details and have list of child nodes under employee.  I have commented the Name child node under employeeà ID 101 and Tel child node under employeeà ID 103. You will get the xml structure as as shown below and highlighted part, Name & Tel node is missing.


Child node exists check:

Then now we are going to load the above xml data into gridview those which are all employee nodes have all child nodes, see the below check.


var vrEmp = (from det in xdoc.Descendants("Company").Descendants("Employee")

where det.Descendants("ID").Any()

&& det.Descendants("Name").Any()

&& det.Descendants("Job").Any()

&& det.Descendants("Tel").Any()

&& det.Descendants("Place").Any()

&& det.Descendants("zip").Any()

select new

{

ID = det.Element("ID").Value,

Name = det.Element("Name").Value,

Job = det.Element("Job").Value,

Tel = det.Element("Tel").Value,

Place = det.Element("Place").Value,

Zip = det.Element("zip").Value,

});

 

GVEmp.DataSource = vrEmp;

GVEmp.DataBind();


 

 

In the above code snippet we are verifying nodes are exists in xml using ANY(), this verifies whether a sequence contains any elements or not. Now the above query returns only two rows such as id-100 & 102 as like below.

Hope the above sample will help you how to verify the element in the sequence whether exists or not before selecting values .

 

User Responses | Post Article

 No Response found!
  Most viewed Articles
 Submit feedback about this article
Please sign in to post feedback