Getting Started with Adobe After Effects - Part 6: Motion Blur


Upload Image Close it
Select File

Browse by Tags · View All
SQL Server 119
#SQLServer 88
Oracle 70
#SQL SERVER 35
BRH 31
SQL Server 2012 29
denali 23
#TSQL 19
TSQL 19
C# 15

Archive · View All
October 2011 31
November 2011 30
September 2011 30
August 2011 18
December 2011 15
July 2011 13
June 2011 8
May 2012 4
April 2012 3
January 2010 3

Filter attribute values from XML file and display the result using XDocument

May 11 2012 12:00AM by Niladri Biswas   

Suppose we have the below XML file

<?xml version="1.0" encoding="UTF-8"?>
<Countries>
	<India>
		<Player isPlaying="1">
		  <Name>Z. Tendulkar</Name>
		  <Age>23</Age> 
		</Player>
		<Player isPlaying="0">
		  <Name>S. Khan</Name>
		  <Age>30</Age> 
		</Player>
	</India>
	<Australia>
		<Player isPlaying="1">
		  <Name>B. Warne</Name>
		  <Age>40</Age>  
		</Player>
		<Player isPlaying="1">
		  <Name>P. Gooch</Name>
		  <Age>30</Age> 
		</Player>
	</Australia>
</Countries>

We need to display the player information for those players who will play for the game (i.e. isPlaying attribute of Player entity should be 1) for a particular country.

Here is my solution for the same

string countryName = "India"; 
string inputXml = @"<?xml version="1.0" encoding="UTF-8"?>
<Countries>
	<India>
		<Player isPlaying="1">
		  <Name>Z. Tendulkar</Name>
		  <Age>23</Age> 
		</Player>
		<Player isPlaying="0">
		  <Name>S. Khan</Name>
		  <Age>30</Age> 
		</Player>
	</India>
	<Australia>
		<Player isPlaying="1">
		  <Name>B. Warne</Name>
		  <Age>40</Age>  
		</Player>
		<Player isPlaying="1">
		  <Name>P. Gooch</Name>
		  <Age>30</Age> 
		</Player>
	</Australia>
</Countries>";
XDocument xmlSkuDescDoc = null;
xmlSkuDescDoc = XDocument.Parse(inputXml); // use XDocument.Load(filename) if loading from file
(from data in xmlSkuDescDoc.Descendants(countryName)
 select data)
 .Descendants("Player")
 .Where(el => el.Attribute("isPlaying").Value == "1")
 .Elements()
 .ToList()
 .ForEach(i => Console.WriteLine("{0} {1}",i.Name.ToString(),i.Value));

Output

Name Z. Tendulkar
Age 23

N.B.~ Please add the namespace System.Xml.Linq

Hope this will be helpful.

Tags: XDocument , C#


Niladri Biswas
7 · 21% · 6720
3
 
0
Lifesaver
 
0
Refreshed
 
0
Learned
 
0
Incorrect



Submit

Your Comment


Sign Up or Login to post a comment.

"Filter attribute values from XML file and display the result using XDocument " rated 5 out of 5 by 3 readers
Filter attribute values from XML file and display the result using XDocument , 5.0 out of 5 based on 3 ratings
    Copyright © Rivera Informatic Private Ltd Contact us      Privacy Policy      Terms of use      Report Abuse      Advertising      [ZULU1097]